public void move(OknoGry okno, Paddle belka) { if (visible) { x += speedX; y += speedY; if (x < 0) { x = 0; speedX *= -1; } else if (x + width > okno.Width) { x = okno.Width - width; speedX *= -1; } if (((x > belka.getX() && x < belka.getX() + belka.getWidth()) || (x + width > belka.getX() && x + width < belka.getX() + belka.getWidth())) && (y + height > belka.getY() && y + height < belka.getY() + belka.getHeight()))//warunek zderzenia z belka { applyBonus(bonus, belka, okno.pilka, okno.lista, okno); dezaktywuj(); } else if (y + height > okno.Height) { dezaktywuj(); } } }
public OknoGry() { InitializeComponent(); h = this.CreateGraphics(); surface = new Bitmap(Width, Height); g = Graphics.FromImage(surface);//surface - bufor, g-obsluga bufora, h-obsluga formy pilkaBit.MakeTransparent(Color.White); belka = new Paddle(Width / 2 - belkaBit.Width / 2, Height - belkaBit.Height - 40, belkaBit.Width, belkaBit.Height, belkaBit); pilka = new Ball(Width / 2, Height - belkaBit.Height - 40 - pilkaBit.Height, 20, 20, pilkaBit, true, 6, -6); }
private void applyBonus(int bonus, Paddle belka, Ball pilka, List<Brick>lista, OknoGry okno) { if (bonus == 2) { belka.setWidth(belka.getWidth() + 50); Message bonusInfo = new Message(okno.Width / 2, okno.Height / 2, "Zwiększenie belki!", 500, false, Color.Black); okno.listaMessage.Add(bonusInfo);//dodanie wiadomości } else if (bonus == 3) { pilka.setSpeedX(pilka.getSpeedX() + 3); pilka.setSpeedY(pilka.getSpeedY() + 3); Message bonusInfo = new Message(okno.Width / 2, okno.Height / 2, "Przyspieszenie!", 500, false, Color.Black); okno.listaMessage.Add(bonusInfo);//dodanie wiadomości } else if (bonus == 4) { pilka.setSpeedX(pilka.getSpeedX() - 3); pilka.setSpeedY(pilka.getSpeedY() - 3); if (pilka.getSpeedX() == 0) pilka.setSpeedX(-1); if (pilka.getSpeedY() == 0) pilka.setSpeedY(-1);//blokada przed ruchem poziomym i pionowym Message bonusInfo = new Message(okno.Width / 2, okno.Height / 2, "Zmiana kierunku piłki!", 500, false, Color.Black); okno.listaMessage.Add(bonusInfo);//dodanie wiadomości } else if (bonus == 5) { foreach (Brick brick in lista) { brick.setY(brick.getY() + brick.getHeight()); } Message bonusInfo = new Message(okno.Width / 2, okno.Height / 2, "Obniżenie stropu!", 500, false, Color.Black); okno.listaMessage.Add(bonusInfo);//dodanie wiadomości } else if (bonus == 6) { foreach (Brick brick in lista) { brick.setHeight(brick.getHeight() / 2); } Message bonusInfo = new Message(okno.Width / 2, okno.Height / 2, "Zmiana rozmiaru kafelków", 500, false, Color.Black); okno.listaMessage.Add(bonusInfo);//dodanie wiadomości } else if (bonus == 7) { foreach (Brick brick in lista) { brick.setHeight(brick.getHeight() * 3 / 2); } Message bonusInfo = new Message(okno.Width / 2, okno.Height / 2, "Zwiększenie rozmiaru kafelków",500, false, Color.Black); okno.listaMessage.Add(bonusInfo);//dodanie wiadomości } }
public void move(Paddle belka, OknoGry okno) { if (!okno.gra) { x = belka.getX() + belka.getWidth() / 2 - width / 2; } else { x += speedX; y += speedY;//poruszanie sie pilki if (x < 0) { x = 0; speedX *= -1; } else if (x > okno.Width - width) { x = okno.Width - width; speedX *= -1; } if (y < 0) { y = 0; speedY *= -1; } else if (y > okno.Height - height) { okno.gra = false; setStart(belka, okno); Message poziom = new Message(okno.Width / 2, okno.Height / 2, "Tracisz zycie!", 1000, false, Color.Black); okno.listaMessage.Add(poziom);//dodanie wiadomości okno.pilka.setBalls(okno.pilka.getBalls() - 1); if (okno.pilka.getBalls() <= 0) { MessageBox.Show("Przegrana!"); Application.Exit(); } }//sprawdzenie czy nie wychodzi poza ramy if(this*belka>0) { speedY *= -1; }//kolizja z belka } }
public bool Move(Wall wall, Paddle paddle) { if (Visible == false) { return(false); } X = X + XVelocity; Y = Y + YVelocity; //check for wall hits if (X < 1) { X = 1; XVelocity = XVelocity * -1; PlaySound(gameContent.wallBounceSound); } if (X > ScreenWidth - Width + 5) { X = ScreenWidth - Width + 5; XVelocity = XVelocity * -1; PlaySound(gameContent.wallBounceSound); } if (Y < 1) { Y = 1; YVelocity = YVelocity * -1; PlaySound(gameContent.wallBounceSound); } if (Y + Height > ScreenHeight) { Visible = false; Y = 0; PlaySound(gameContent.missSound); return(false); } //check for paddle hit //paddle is 70 pixels. we'll logically divide it into segments that will determine the angle of the bounce Rectangle paddleRect = new Rectangle((int)paddle.X, (int)paddle.Y, (int)paddle.Width, (int)paddle.Height); Rectangle ballRect = new Rectangle((int)X, (int)Y, (int)Width, (int)Height); if (HitTest(paddleRect, ballRect)) { PlaySound(gameContent.paddleBounceSound); int offset = Convert.ToInt32((paddle.Width - (paddle.X + paddle.Width - X + Width / 2))); offset = offset / 5; if (offset < 0) { offset = 0; } switch (offset) { case 0: XVelocity = -6; break; case 1: XVelocity = -5; break; case 2: XVelocity = -4; break; case 3: XVelocity = -3; break; case 4: XVelocity = -2; break; case 5: XVelocity = -1; break; case 6: XVelocity = 1; break; case 7: XVelocity = 2; break; case 8: XVelocity = 3; break; case 9: XVelocity = 4; break; case 10: XVelocity = 5; break; default: XVelocity = 6; break; } YVelocity = YVelocity * -1; Y = paddle.Y - Height + 1; return(true); } bool hitBrick = false; for (int i = 0; i < 7; i++) { if (hitBrick == false) { for (int j = 0; j < 10; j++) { Brick brick = wall.BrickWall[i, j]; if (brick.Visible) { Rectangle brickRect = new Rectangle((int)brick.X, (int)brick.Y, (int)brick.Width, (int)brick.Height); if (HitTest(ballRect, brickRect)) { PlaySound(gameContent.brickSound); brick.Visible = false; Score = Score + 7 - i; YVelocity = YVelocity * -1; bricksCleared++; hitBrick = true; break; } } } } } return(true); }
public void Update(KeyboardState kstate, GameTime gameTime, Level level, Paddle paddle) { if (kstate.IsKeyDown(Keys.Space)) { Launc = true; vector.Y = -1; vector.Normalize(); } Move(vector.X * speed * (float)gameTime.ElapsedGameTime.TotalSeconds, vector.Y * speed * (float)gameTime.ElapsedGameTime.TotalSeconds); if (Launc) { if ((GetPosition().Y) <= 32) { vector.Y = -vector.Y; Move(vector.X * speed * (float)gameTime.ElapsedGameTime.TotalSeconds, vector.Y * speed * (float)gameTime.ElapsedGameTime.TotalSeconds); } if (((GetPosition().X + Width) >= (game.Width + game.X0)) || (GetPosition().X <= game.X0)) { vector.X = -vector.X; Move(vector.X * speed * (float)gameTime.ElapsedGameTime.TotalSeconds, vector.Y * speed * (float)gameTime.ElapsedGameTime.TotalSeconds); } if (GetPosition().Y >= game.GraphicsDevice.DisplayMode.Height) { vector.X = 0; vector.Y = 0; Launc = false; game.gameProcess.helth--; if (game.gameProcess.helth <= 0) { game.gameProcess.Reset(); } } if (CheckCollided(paddle)) { MoveTo(GetPosition().X, paddle.GetPosition().Y - Height); float racketCenter = paddle.GetPosition().X + (paddle.Width / 2); float ballCenter = GetPosition().X + (Width / 2); float normalizedDistance = Math.Abs(ballCenter - racketCenter) / (paddle.Width / 2); if (vector.X < 0) { vector.X = -normalizedDistance; } else { vector.X = normalizedDistance; } vector.Y = -1 * vector.Y; vector.Normalize(); Move(vector.X * speed * (float)gameTime.ElapsedGameTime.TotalSeconds, vector.Y * speed * (float)gameTime.ElapsedGameTime.TotalSeconds); } foreach (Block block in level) { if (CheckCollided(block)) { if (((block.GetPosition().X + block.Width) >= (GetPosition().X + Width)) && (block.GetPosition().X <= GetPosition().X)) { vector.Y = -vector.Y; } else if (((block.GetPosition().Y + block.Height) >= (GetPosition().Y + Height)) && ((block.GetPosition().Y) <= (GetPosition().Y))) { vector.X = -vector.X; } else { vector.X = -vector.X; vector.Y = -vector.Y; } block.Hit(); vector.Normalize(); Move(vector.X * speed * (float)gameTime.ElapsedGameTime.TotalSeconds, vector.Y * speed * (float)gameTime.ElapsedGameTime.TotalSeconds); } } } }
void Awake() { instance = this; }
public void setStart(Paddle belka, OknoGry okno) { setX(belka.getX() + belka.getWidth() / 2 - width / 2); setY(okno.Height - belka.getHeight() - height - 40); setSpeedX(6); setSpeedY(-6); belka.setWidth(belka.getBitmap().Width); }