public void Move(Window window, Ball ball, int h) { if (window.GetKey (KeyCode.Up)) { this.y -= this.speed; } if (window.GetKey (KeyCode.Down)) { this.y += this.speed; } if (this.y <= 14) this.y += this.speed; if (this.y >= h - this.height - 15) this.y -= this.speed; if ((ball.GetX () == this.x - 15) && (ball.GetY () >= this.y) && (ball.GetY () <= this.y + this.height)) { ball.SetDirezioneX (ball.GetDirezioneX () * -1); if ((ball.GetY () < this.y + (height / 5))) ball.SetDirezioneY (-2); else if ((ball.GetY () < (this.y + (height / 5) * 2))) ball.SetDirezioneY (-1); else if ((ball.GetY () < (this.y + (height / 5) * 3))) ball.SetDirezioneY (0); else if ((ball.GetY () < (this.y + (height / 5) * 4))) ball.SetDirezioneY (1); else ball.SetDirezioneY (2); } }
public void Update(Window window, Bar bar1, Bar bar2, Ball ball) { //Disegno parte superiore del Ring this.DrawRectFilled (window, 10, 10, 255, 255, 255, window.width-20, 10); //Disegno parte inferiore del Ring this.DrawRectFilled (window, 10, window.height-20, 255, 255, 255, window.width-20, 10); //Disegno righe tratteggiate che separano il ring int k = 20; while(k < window.height-20) { this.DrawRectFilled(window, window.width/2, k, 255, 255, 255, 7, 30); k += 40; } //Disegno la Palla //this.DrawRectFilled (window, ball.GetX(), ball.GetY(), ball.GetR(), ball.GetG(), ball.GetB(), ball.GetWidht(), ball.GetHeight()); this.DrawCircle(window, ball.GetX(), ball.GetY(), ball.GetRaggio(), 255, 255, 255); //Disegno la barra del Player 1 this.DrawRectFilled (window, bar1.GetX(), bar1.GetY(), 255, 0, 0, bar1.GetWidht(), bar1.GetHeight()); //Disegno la Barra del Player 2 this.DrawRectFilled (window, bar2.GetX(), bar2.GetY(), 0, 255, 0, bar2.GetWidht(), bar2.GetHeight()); //Movimeto barra del Player 1 bar1.Move (window, ball); //Movimeto barra del Player 2 bar2.Move2 (window, ball, window.height); //Movimeto della Palla ball.Update (window.height, window.width); numb1.CurrentResultDec (window, bar1.GetPoints()); numb2.CurrentResult (window, bar1.GetPoints()); numb3.CurrentResultDec (window, bar2.GetPoints()); numb4.CurrentResult (window, bar2.GetPoints()); }
//private ResultNumber numb3; public Ring (Window window) { numb1 = new ResultNumber (window.width/2 - 170, 30, true ); numb2 = new ResultNumber (window.width/2 - 100, 30, true ); numb3 = new ResultNumber (window.width/2+5, 28, true ); numb4 = new ResultNumber (window.width / 2+5, 28, false ); }
public static void Main (string[] args) { Window window = new Window (1200, 700, "RingPong", PixelFormat.RGB); //Definizione : Ring, Finestra del programma, Palla, Barra Player 1, Barra Player 2 Ring ring = new Ring (window); Ball ball = new Ball (); //Parametri Bar : Posizione X, Posizione Y, Comando per andare su, Comando per andare giù Bar bar1= new Bar (window.width-25, window.height/2, KeyCode.Up, KeyCode.Down); Bar bar2 = new Bar (10, window.height/2, KeyCode.W, KeyCode.S); //Inizio Gioco bool isGameRunning = true; while (isGameRunning) { //Pulizia della Finestra ring.Clear(window, 0, 0, 0); ring.Update (window, bar1, bar2, ball); window.Blit(); //Scrittura del punteggio if (bar1.GetPoints () == 15) { Console.WriteLine ("Player 1 VINCE"); isGameRunning = false; } else if (bar2.GetPoints () == 15) { Console.WriteLine ("Player 2 VINCE"); isGameRunning = false; } } }
public void DrawVertLine(Window window, int x, int y, byte r, byte g, byte b, int h) { for (int posY = y; posY < h + y; posY++) { PutPixel (window, x, posY, r, g, b); } }
public static void PutPixel (Window window, int x, int y, byte r, byte g, byte b) { if (x < 0 || x > window.width - 1 || y < 0 || y > window.height - 1) return; int pos = (y * 3 * window.width) + x*3; window.bitmap[pos] = r; window.bitmap[pos+1] = g; window.bitmap[pos+2] = b; }
public void Clear(Window window, byte r, byte g, byte b) { for(int y = 0; y < window.height; y++) { for (int x = 0; x < window.width; x++) { PutPixel (window, x, y, r, g, b); } } }
//In questo metodo facciamo gli UPDATE della barra del player 1 public void Move(Window window, Ball ball) { if(window.GetKey(this.up)) { this.y -= this.speed; } if(window.GetKey(this.down)) { this.y += this.speed; } //Controllo pe non superare il bordo superiore if (this.y <= 14) this.y = 20; //Controllo per non superare il bordo inferiore if (this.y >= window.height-this.height-17) this.y = window.height - this.height-20; //int distance = (int)(ball.GetRaggio() * 26.67)/100; //Controllo il punto della barra in cui è avvenuto il contatto con la pallina per far cambiare l'angolazione del tiro if ((ball.GetX () + ball.GetRaggio()-17 >= this.x - 15) && (ball.GetY () >= this.y) && (ball.GetY () <= this.y + this.height)) { ball.SetDirezioneX (ball.GetDirezioneX () * -1); ball.SetR(255); ball.SetG(0); ball.SetB(0); if ((ball.GetY () < this.y + (height / 5))) ball.SetDirezioneY (-2); else if ((ball.GetY () < (this.y + (height / 5) * 2))) ball.SetDirezioneY (-1); else if ((ball.GetY () < (this.y + (height / 5) * 3))) ball.SetDirezioneY (0); else if ((ball.GetY () < (this.y + (height / 5) * 4))) ball.SetDirezioneY (1); else ball.SetDirezioneY (2); } else if (ball.GetX () > window.width) { points++; ball.SetX (window.width/2); ball.SetY (window.height/2); if (RandomGenerator.GetRandom (0, 1) == 0) ball.SetDirezioneX (-1); else ball.SetDirezioneX (1); ball.SetDirezioneY (RandomGenerator.GetRandom (-2, 2)); } }
public void CurrentResult(Window window, int result) { switch (result) { case 20: case 0: { indexX = 2; indexY = 4; break; } case 11: case 1: { indexX = 3; indexY = 4; break; } case 12: case 2: { indexX = 4; indexY = 4; break; } case 13: case 3: { indexX = 5; indexY = 4; break; } case 14: case 4: { indexX = 0; indexY = 5; break; } case 15: case 5: { indexX = 1; indexY = 5; break; } case 16: case 6: { indexX = 2; indexY = 5; break; } case 17: case 7: { indexX = 3; indexY = 5; break; } case 18: case 8: { indexX = 4; indexY = 5; break; } case 19: case 9: { indexX = 5; indexY = 5; break; } case 10: { indexX = 2; indexY = 4; if (!change) { change = true; x += 70; } break; } default: break; } Ring.DrawSprite (window, sprite, x, y, sprite.width/6*indexX, sprite.height/7*indexY, sprite.width/6, sprite.height/7); }
public void CurrentResultDec(Window window, int result) { switch (result) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: { indexX = 1; indexY = 4; break; } case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: { indexX = 3; indexY = 4; break; } case 20: case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 28: case 29: { indexX = 4; indexY = 4; break; } default: break; } Ring.DrawSprite (window, sprite, x, y, sprite.width/6*indexX, sprite.height/7*indexY, sprite.width/6, sprite.height/7); }
public void DrawCircle(Window window, int j, int k, int radius, byte r, byte g, byte b) { for (int x = - radius; x < radius; x++) { int height = (int)Math.Sqrt(radius * radius - x * x); for (int y = -height; y < height; y++) { //if (y == -height || y == height - 1 || x == -radius || x == radius - 1) PutPixel(window, x + j, y + k, r, g, b); } } }
//Stampa rettangoli pieni, parametri: finestra, x, y, RGB, larghezza, altezza public void DrawRectFilled(Window window, int x, int y, byte r, byte g, byte b, int z, int h) { for(int i = 0; i < z; i++) DrawVertLine(window, x+i, y, r, g, b, h); }
public static void DrawSprite(Window window, Sprite sprite, int xpos, int ypos, int xoffset, int yoffset, int width, int height) { for (int y = yoffset; y < yoffset + height; y++) { for (int x = xoffset; x < xoffset + width; x++) { int position = (y * sprite.width * 4) + (x * 4); byte r = sprite.bitmap[position]; byte g = sprite.bitmap[position + 1]; byte b = sprite.bitmap[position + 2]; byte a = sprite.bitmap[position + 3]; if (a == 255) PutPixel(window, xpos + x - xoffset, ypos + y - yoffset, r, g, b); } } }
/// <summary> /// The Main Loop /// </summary> public static void Main() { using (Window win = new Window()) win.Run(); }