Ich habe das GROSS Problem



  • Ich habe das groß Problem. Ich habe das (einige Variablen sind nicht gebraucht):

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace GameTest2
    {
    	public partial class Form1 : Form
    	{
    		//Setting up all misc things required to run the program
    		private const int DefaultWidth = 256;
    		private const int DefaultHeight = 192;
    		private int Scaler = 4;
    		[DllImport("user32.dll")]
    		public static extern int GetKeyboardState(byte[] keystate);
    		private Bitmap buffer;
    		private byte[] keys;
    		private Timer synchronizer;
    		private Timer framerateUpdater;
    		private int framerate;
    
            //Declaring all strings/variables
            double ms = 64;
            double creditlines = 44;
            double[] tgen = new double[5];
            string[] splash = new string[59];
            double[,,] map = new double[64, 64, 2];
            double[,] block1 = new double[16, 16];
            double[,] block2 = new double[16, 16];
            double[,] block3 = new double[16, 16];
            double[,] block4 = new double[16, 16];
            double[,] block5 = new double[16, 16];
            double[,] block6 = new double[16, 16];
            double[] b = new double[6];
            string[] credits = new string[44];
            double[] breakable = new double[12];
            double[,] screenshot = new double[256, 192];
            double[] mob_x = new double[5];
            double[] mob_y = new double[5];
            double[] mob_direction = new double[5];
            double[] mob_layer = new double[5];
            double menu = 1;
            double ca = 0;
            double cx = 0;
            double cy = 0;
            double mst = 0.2;
            double mob_count = 1;
            double mob_enabled = 1;
            double mod_enabled = 1;
            string vers = "Beta 1.9.0";
            string sss = "On";
            string mss = "Off";
            string rss = "On";
            string tss = "Day";
            double tds = 1;
            double o = 4095 / 4096;
            double block = 1;
            double camx = 6;
            double camy = 14.5;
            double camz = 0.5;
            double cama = 0;
            double camb = 0;
            double q = 1;
            double touching = 0;
            double jumping = 0;
            double velz = 0;
            double walking = 0;
            double x;
            double y;
            double dx;
            double dy;
            double angstep;
            double currang;
            double c;
            double s;
            double dist;
            double collx;
            double colly;
            double t;
            double slice;
            double i;
            double j;
            double p;
            double h;
            int[,] texture = new int[8, 8];
    
    		public Form1()
    		{
    			InitializeComponent();
    			InitializeVariables();
    
    			KeyDown += new KeyEventHandler(Form1_KeyDown);
    			KeyUp += new KeyEventHandler(Form1_KeyUp);
    
    			BackgroundImage = buffer;
    			BackgroundImageLayout = ImageLayout.Zoom;
    			DoubleBuffered = true;
    
                for (int i = 0; i < 64; i++)
                {
                    for (int j = 0; j < 64; j++)
                    {
                        for (int k = 0; k < 2; k++)
                        {
                            map[i, j, k] = 0;
                        }
                    }
                }
                for (double j = 0; j < ms - 1; j = j + ms - 1.0)
                {
                    for (double i = 0; i < ms - 1; i++)
                    {
                        for (double k = 0; k < 2; k++)
                        {
                            map[Convert.ToInt32(i), Convert.ToInt32(j), Convert.ToInt32(k)] = 1;
                        }
                    }
                }
                for (int i = 0; i < 8; i++)
                {
                    for (int j = 0; j < 8; j++)
                    {
                        texture[i, j] = 2;
                    }
                }
    
    			synchronizer.Start();
    			framerateUpdater.Start();
    		}
    
    		void synchronizer_GameLoop(object sender, EventArgs e)
    		{
    			framerate++;
    
                x = 0;
                y = 0;
                dx = 0;
                dy = 0;
    
                if (jumping == 0)
                    goto notjumping;
                camz = camz + velz;
                velz = velz - 0.06;
                if (camz < 0.5){
                    camz = 0.5;
                    jumping = 0;
                }
                goto jumpend;
    
    notjumping:
                walking = walking + 0.9;
                camz = 0.5 + Math.Abs(Math.Sin(walking)) * 0.15;
                if ((keys[(int)Keys.Q] & 128) == 128)
                {
                    walking = 0;
                    jumping = 1;
                    camz = 0.5;
                    velz = 0.2;
                }
    jumpend:
                if ((keys[(int)Keys.W] & 128) == 128)
                {
                    x += dx;
                    y -= dy;
                }
                if ((keys[(int)Keys.S] & 128) == 128)
                {
                    x -= dx;
                    y -= dy;
                }
                if ((keys[(int)Keys.A] & 128) == 128)
                {
                    x -= dy;
                    y += dx;
                }
                if ((keys[(int)Keys.D] & 128) == 128)
                {
                    x += dy;
                    y -= dx;
                }
    
                if (!Convert.ToBoolean(map[Convert.ToInt32(x), Convert.ToInt32(y), 0]))
                {
                    camx = x;
                    camy = y;
                }
    
    			using (Graphics graphics = Graphics.FromImage(buffer))
    			{
    				graphics.FillRectangle(Brushes.Black, 0, 0, buffer.Width, buffer.Height);
    				graphics.FillRectangle(Brushes.SkyBlue, 0, 0, buffer.Width, (int)(buffer.Height / 2 - camb - 1));
    				graphics.FillRectangle(Brushes.Green, 0, (int)(buffer.Height / 2 - camb), buffer.Width, buffer.Height);
    				double angstep = Math.PI * q / 3 / 256;
    				double currang = Math.PI / 6;
    				for (int i = 0; i < buffer.Width; i += (int)q)
    				{
    					double c = Math.Cos(cama + currang);
    					double s = Math.Sin(cama + currang);
    					if (Math.Abs(c) > Math.Abs(s))
    					{
    						dx = Math.Sign(c);
    						dy = s / Math.Abs(c);
    						x = Math.Floor(camx) + o * Convert.ToInt32(c < 0);
    						y = camy - Math.Abs(x - camx) * dy;
    						//CASTXLOOP
    						do													
    						{
    							x += dx;
    							y += dy;
    						} while ((map[(int)Math.Floor(x), (int)Math.Floor(y), 0] == 0 && map[(int)Math.Floor(x - dx),(int)Math.Floor(y), 0] == 0));
    						t = map[(int)Math.Floor(x - dx), (int)Math.Floor(y), 0] - 1;
    						if (t >= 0)
    						{
    							colly = Math.Floor(y) + o * Convert.ToInt32(s < 0);
    							collx = x - (y - colly) / dy * dx;
    							slice = Math.Abs(collx % 1 - o * Convert.ToInt32(s < 0));
    						}
    						//CASTXCOLL
    						else
    						{
    							colly = y;
    							collx = x;
    							t = map[(int)Math.Floor(x), (int)Math.Floor(y), 0] - 1;
    							slice = Math.Abs(colly % 1 - o * Convert.ToInt32(c > 0));
    						}
    					}
    					//CASTY
    					else
    					{
    						dy = Math.Sign(s);
    						dx = c / Math.Abs(s);
    						y = Math.Floor(camy) + o * Convert.ToInt32(s < 0);
    						x = camx - Math.Abs(y - camy) * dx;
    						//CASTYLOOP
    						do
    						{
    							x += dx;
    							y += dy;
    						} while (map[(int)Math.Floor(x), (int)Math.Floor(y), 0] == 0 && map[(int)Math.Floor(x), (int)Math.Floor(y-dy), 0] == 0);
    						t = map[(int)Math.Floor(x), (int)Math.Floor(y-dy), 0] - 1;
    						if (t >= 0)
    						{
    							collx = Math.Floor(x) + o * Convert.ToInt32(c < 0);
    							colly = y - (x - collx) / dx * dy;
    							slice = Math.Abs(colly % 1 - o * Convert.ToInt32(c > 0));
    						}
    						//CASTYCOLL
    						else
    						{
    							colly = y;
    							collx = x;
    							t = map[(int)Math.Floor(x), (int)Math.Floor(y), 0]-1;
    							slice = Math.Abs(collx % 1 - o * Convert.ToInt32(s < 0));
    						}
    					}
    					dx = collx - camx;
    					dy = colly - camy;
    					double dist = Math.Sqrt(dx * dx + dy * dy);
    					if (dist == 0)
    						dist = 1.0 / 4096;
    					h = 24.0 / dist / Math.Cos(currang);
    					if (h > 2500)
    						h = 2500;
    					t = (int)(t + slice) * 8;
    					j = i + q - 1;
    					p = buffer.Height / 2 - camb + h * 8 * camz - h;
    
    					graphics.FillRectangle(texture[Convert.ToInt32(t), 7] == 1 ? Brushes.LightGray : Brushes.DarkRed, i, (int)p, (int)j - i, (int)h);
    					graphics.FillRectangle(texture[Convert.ToInt32(t), 6] == 1 ? Brushes.LightGray : Brushes.DarkRed, i, (int)(p - h), (int)j, (int)h);
    					p = p - h * 2;
    					graphics.FillRectangle(texture[Convert.ToInt32(t), 5] == 1 ? Brushes.LightGray : Brushes.DarkRed, i, (int)p, (int)j - i, (int)h);
    					graphics.FillRectangle(texture[Convert.ToInt32(t), 4] == 1 ? Brushes.LightGray : Brushes.DarkRed, i, (int)(p - h), (int)j, (int)h);
    					p = p - h * 2;
    					graphics.FillRectangle(texture[Convert.ToInt32(t), 3] == 1 ? Brushes.LightGray : Brushes.DarkRed, i, (int)p, (int)j - i, (int)h);
    					graphics.FillRectangle(texture[Convert.ToInt32(t), 2] == 1 ? Brushes.LightGray : Brushes.DarkRed, i, (int)(p - h), (int)j, (int)h);
    					p = p - h * 2;
    					graphics.FillRectangle(texture[Convert.ToInt32(t), 1] == 1 ? Brushes.LightGray : Brushes.DarkRed, i, (int)p, (int)j - i, (int)h);
    					graphics.FillRectangle(texture[Convert.ToInt32(t), 0] == 1 ? Brushes.LightGray : Brushes.DarkRed, i, (int)(p - h), (int)j, (int)h);
    				}
    
    				graphics.FillRectangle(Brushes.Red, (int)x, (int)y, 1, 1);
    			}
    		}
    
    		void Form1_KeyDown(object sender, KeyEventArgs e)
    		{
    			GetKeyboardState(keys);
    		}
    
    		void Form1_KeyUp(object sender, KeyEventArgs e)
    		{
    			GetKeyboardState(keys);
    		}
    
    		void InitializeVariables()
    		{
    			keys = new byte[256];
    			buffer = new Bitmap(DefaultWidth * Scaler, DefaultHeight * Scaler);
    			synchronizer = new Timer();
    			framerateUpdater = new Timer();
    
    			synchronizer.Tick += new EventHandler(synchronizer_GameLoop);
    			synchronizer.Interval = 8;
    
    			framerateUpdater.Tick += new EventHandler(framerateUpdater_Tick);
    			framerateUpdater.Interval = 1000;
    		}
    
    		void framerateUpdater_Tick(object sender, EventArgs e)
    		{
    			label1.Text = framerate.ToString();
    			framerate = 0;
    		}
    	}
    }
    

    Und mein Array (map[]) ist Überlauft! Das Programm ist der Haufen aus SmileBASIC, aber es nicht arbeitet hier. Wie? Das Fehler ist an 216.



  • Sorry to say (or to write), but your code is as awful as your german.
    You have too much class member variables (instead of using them as local variables). And your code looks more like "C" than "C#" (is it a port from it?).

    And please write in english - so we can understand you...





  • Let your code run in the debugger, let the debugger break on all "CLR Exceptions", and then have a look at what values your variables have (i.e. what index is being accessed).
    Then refactor and clean up your code for about twice as long as it took you to write it.
    And try again.
    Then you should be in a position to find out what went wrong, why, and most importantly: how to fix it.

    And before you ask: all of the above are also your task, and not something that we will do for you. And the reason is not that we're bad people or unwilling to help. The reason is that your code is very very bad, and that you (probably) won't find anyone who's willing to debug such a terrible piece of code for you.

    And please write in English. I for one would have far less trouble understanding you then I have when you write in German (if one can call it that).



  • English or German language doesn´t really matter. 🙂

    The given code is a wonderful mixture of ancient BASIC-spaghetti-code combined with newer elements of C and C#

    1. Consider first the do-while-loop and the array map[]. You told us there is an overflow
    2. Understand the algorithms and code-blocks to rewrite the program totally new

    Maybe debugging cannot help you. Then try it this way:

    int  it= 0;
    bool go = true; 
    do
    {
      it++;
      if (it > 20) go = false;  // to avoid a dead-do-loop when testing
      // ....
      // calculate the conditions resulting go
      // see what happens
      MessageBox.Show("\ncondition1   " + condition1.ToString() +
                      "\ncondition2   " + condition2.ToString() +
                      "\n....." +
                      "\ngo           " + go.Tostring() +
                      "");
    } while go;
    

    GOOD LUCK! 🕶


Anmelden zum Antworten