Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GAME
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using SFML.Audio;
- using SFML.Graphics;
- using SFML.System;
- using SFML.Window;
- namespace Siatkowkaa2
- {
- public class Game
- {
- enum GameState
- {
- VsAI,
- VsPlayer,
- Menu,
- WinningScreen,
- Closing
- };
- public RenderWindow Window = new RenderWindow(new VideoMode(1000, 600), "Siatkowka", Styles.Titlebar | Styles.Close);
- public const float TICK = 0.03F;
- private GameState gameState;
- // ########################### OBIEKTY W GRZE ##########################
- private Player Player = new Player(new Vector2f(750, 462.5F), new Vector2f(100, 175), 8, 7);
- private Player Player2;
- private Player NotAI = new Player(new Vector2f(250, 462.5F), new Vector2f(100, 175), 8, 7);
- private Player AI1 = new Player(new Vector2f(250, 462.5F), new Vector2f(100, 175), 6, 7);
- private Player AI2 = new Player(new Vector2f(230, 462.5F), new Vector2f(100, 175), 8, 7);
- private Player AI3 = new Player(new Vector2f(250, 462.5F), new Vector2f(150, 230), 8, 6);
- private Ball Ball = new Ball(80, new Vector2f(750, 220));
- private Net Net = new Net(new Vector2f(15, 350));
- // ############################ TEKSTURY ################################
- private Texture BackgroundTex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/background.png");
- private Texture PlayerTex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/PlayerTextures/tex.png");
- private Texture BallTex = new Texture(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Textures\ball2.png");
- private Texture Player2Tex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/PlayerTextures/tex.png");
- private Texture NetTex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/net2.png");
- private Texture MenuBackgroundTex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/menubackground.png");
- private Texture GraczVsGraczTex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/graczvsgracz1.png");
- private Texture GraczVsKomputerTex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/graczvskomputer.png");
- private Texture ExitButtonTex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/exitbutton.png");
- private Texture Button1Tex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/1.png");
- private Texture Button2Tex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/2.png");
- private Texture Button3Tex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/3.png");
- private Texture LevelButtonTex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/poziom.png");
- private Texture BallHelpTex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/ball2.png");
- private Texture PointsButtonTex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/maxpunkty.png");
- private Texture Points15ButtonTex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/15.png");
- private Texture Points20ButtonTex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/20.png");
- private Texture Points25ButtonTex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/25.png");
- private Texture EndGameTex = new Texture(@"F:/Visual/Siatkowka/Siatkowka/Textures/endgame.png");
- private Font ScoreFont = new Font(@"F:/Visual/Siatkowka/Siatkowka/scoreboard.ttf");
- // ############################ SPRITE'Y ################################
- private Sprite PlayerSprite = new Sprite();
- private Sprite Player2Sprite = new Sprite();
- private Sprite Background = new Sprite();
- private Sprite MenuBackground = new Sprite();
- private Sprite GraczVsGracz = new Sprite();
- private Sprite GraczVsKomputer = new Sprite();
- private Sprite ExitButton = new Sprite();
- private Sprite Button1 = new Sprite();
- private Sprite Button2 = new Sprite();
- private Sprite Button3 = new Sprite();
- private Sprite LevelButton = new Sprite();
- private Sprite PointsButton = new Sprite();
- private Sprite Points15Button = new Sprite();
- private Sprite Points20Button = new Sprite();
- private Sprite Points25Button = new Sprite();
- private Sprite EndGame = new Sprite();
- private RectangleShape BallHelp = new RectangleShape();
- // ############################## DZWIEKI ###############################
- private SoundBuffer WinningGameBuf = new SoundBuffer(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\winninggame.wav");
- private SoundBuffer LosingGameBuf = new SoundBuffer(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\losinggame.wav");
- private Sound WinningGame = new Sound();
- private Sound LosingGame = new Sound();
- private Music GameMusic = new Music(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\backgroundmusic.ogg");
- // ############################## INNE #################################
- private int MaxPoints { get; set; } = 15;
- private int TickCounter;
- private int RandomNumber;
- private int Strenght;
- private int WhoIsActualPlayer = 0;
- private int LeftPlayerTex = 0;
- private int LeftPlayer2Tex = 200;
- private bool FirstTouch = false;
- private bool IsBallOnScreen = true;
- private bool GameOver = false;
- private bool RightIsWinner = false;
- private Vector2f BallLeftPosition = new Vector2f(750, 230);
- private Vector2f BallRightPosition = new Vector2f(250, 230);
- private string Score_s;
- private string winer;
- private Text ScoreText;
- private Text kopa;
- private Time T;
- public Game()
- {
- // ############################# PODPINANIE TESKTUR ##############################
- gameState = GameState.Menu;
- Ball.Texture = BallTex;
- Net.Texture = NetTex;
- Player.Texture = PlayerTex;
- Player.TextureRect = new IntRect(LeftPlayerTex, 0, 200, 375);
- // Player2.Texture = Player2Tex;
- //Player2.TextureRect = new IntRect(LeftPlayer2Tex, 0, 200, 375);
- NotAI.Texture = Player2Tex;
- NotAI.TextureRect = new IntRect(LeftPlayer2Tex, 0, 200, 375);
- AI1.Texture = Player2Tex;
- AI1.TextureRect = new IntRect(LeftPlayer2Tex, 0, 200, 375);
- AI2.Texture = Player2Tex;
- AI2.TextureRect = new IntRect(LeftPlayer2Tex, 0, 200, 375);
- AI3.Texture = Player2Tex;
- AI3.TextureRect = new IntRect(LeftPlayer2Tex, 0, 200, 375);
- PlayerSprite.Texture = PlayerTex;
- Player2Sprite.TextureRect = new IntRect(LeftPlayerTex, 0, 200, 375);
- BallHelp.Texture = BallHelpTex;
- Background.Texture = BackgroundTex;
- Background.Scale = new Vector2f(0.84F, 0.69F);
- EndGame.Texture = EndGameTex;
- EndGame.Scale = new Vector2f(1.613F, 1.818F);
- // ############## WYNIK ################
- ScoreText = new Text(Score_s, ScoreFont);
- ScoreText.Position = new Vector2f (437, 0);
- ScoreText.CharacterSize = 50;
- // ############### KONIEC GRY ###############
- if (gameState == GameState.WinningScreen)
- {
- if (RightIsWinner == true)
- {
- Player.Position = new Vector2f(800, 163);
- Player2.Position = new Vector2f(182, 242);
- }
- else
- {
- Player2.Position = new Vector2f(800, 163);
- Player.Position = new Vector2f(192, 242);
- }
- }
- // ##################### INNE ################################
- GameMusic.Loop = true;
- GameMusic.Volume = 15;
- WinningGame.SoundBuffer = WinningGameBuf;
- LosingGame.SoundBuffer = LosingGameBuf;
- WinningGame.Volume = 50;
- LosingGame.Volume = 50;
- Player2 = NotAI;
- }
- public void ProcEvents()
- {
- Window.Closed += (sender, arg) => Window.Close();
- Window.KeyReleased += Window_KeyReleased;
- void Window_KeyReleased(object sender, KeyEventArgs e)
- {
- if (e.Code == Keyboard.Key.Right && gameState == GameState.Menu)
- {
- LeftPlayerTex += 200;
- if (LeftPlayerTex == -200) LeftPlayerTex = 1800;
- if (LeftPlayerTex == 2000) LeftPlayerTex = 0;
- Player.TextureRect = new IntRect(LeftPlayerTex, 0, 200, 375);
- PlayerSprite.TextureRect = new IntRect(LeftPlayerTex, 0, 200, 375);
- }
- if (e.Code == Keyboard.Key.Left && gameState == GameState.Menu)
- {
- LeftPlayerTex += -200;
- if (LeftPlayerTex == -200) LeftPlayerTex = 1800;
- if (LeftPlayerTex == 2000) LeftPlayerTex = 0;
- Player.TextureRect = new IntRect(LeftPlayerTex, 0, 200, 375);
- PlayerSprite.TextureRect = new IntRect(LeftPlayerTex, 0, 200, 375);
- }
- if (e.Code == Keyboard.Key.D && gameState == GameState.Menu)
- {
- LeftPlayer2Tex += 200;
- if (LeftPlayer2Tex == -200) LeftPlayer2Tex = 1800;
- if (LeftPlayer2Tex == 2000) LeftPlayer2Tex = 0;
- Player2.TextureRect = new IntRect(LeftPlayer2Tex, 0, 200, 375);
- Player2Sprite.TextureRect = new IntRect(LeftPlayerTex, 0, 200, 375);
- NotAI.TextureRect = new IntRect(LeftPlayer2Tex, 0, 200, 375);
- AI1.TextureRect = new IntRect(LeftPlayer2Tex, 0, 200, 375);
- AI2.TextureRect = new IntRect(LeftPlayer2Tex, 0, 200, 375);
- AI3.TextureRect = new IntRect(LeftPlayer2Tex, 0, 200, 375);
- }
- if (e.Code == Keyboard.Key.A && gameState == GameState.Menu)
- {
- LeftPlayerTex += -200;
- if (LeftPlayer2Tex == -200) LeftPlayer2Tex = 1800;
- if (LeftPlayer2Tex == 2000) LeftPlayer2Tex = 0;
- Player2.TextureRect = new IntRect(LeftPlayer2Tex, 0, 200, 375);
- Player2Sprite.TextureRect = new IntRect(LeftPlayerTex, 0, 200, 375);
- NotAI.TextureRect = new IntRect(LeftPlayer2Tex, 0, 200, 375);
- AI1.TextureRect = new IntRect(LeftPlayer2Tex, 0, 200, 375);
- AI2.TextureRect = new IntRect(LeftPlayer2Tex, 0, 200, 375);
- AI3.TextureRect = new IntRect(LeftPlayer2Tex, 0, 200, 375);
- }
- }
- }
- public void Update(Time DeltaTime)
- {
- float DeltaT = DeltaTime.AsSeconds();
- TickCounter++;
- if (TickCounter == 200) TickCounter = 0;
- if (TickCounter % 50 == 0) // losowanie liczby do funkcji DzwiekOdbicia()
- {
- Random Randololo = new Random();
- RandomNumber = Randololo.Next(1, 9);
- }
- // WYSWIETLANIE WYNIKU
- if (Player2.Score > 9) ScoreText.Position = new Vector2f(410, 0);
- Score_s = Player2.Score.ToString() + " : " + Player.Score.ToString();
- ScoreText.DisplayedString = Score_s;
- // ############################# PLAYER 1 PORUSZANIE #######################
- if (gameState == GameState.VsAI || gameState == GameState.VsPlayer)
- {
- if (Player.Position.Y < Player.StartingPosition.Y)
- {
- float FallSpeed = 2;
- Player.MovementVector = Player.MovementVector + new Vector2f(0, 600) * DeltaT * FallSpeed;
- // jesli player jest na ziemi, upewnij sie zeby nie wypadl za ekran
- if ((Player.Position.Y + Player.MovementVector.Y * DeltaT) >= Player.StartingPosition.Y)
- {
- Player.Position = new Vector2f(Player.Position.X, Player.StartingPosition.Y);
- Player.MovementVector = new Vector2f(0, 0);
- }
- }
- if (Player.Position.Y > Player.StartingPosition.Y) // po punkcie
- {
- Player.Position = new Vector2f(Player.Position.X, Player.StartingPosition.Y);
- Player.MovementVector = new Vector2f(0, 0);
- }
- // skakanie
- else if (Keyboard.IsKeyPressed(Keyboard.Key.Up) && Player.Position.Y == Player.StartingPosition.Y)
- {
- Player.MovementVector = new Vector2f(0, -700);
- }
- // sterowanie lewo prawo
- if (Keyboard.IsKeyPressed(Keyboard.Key.Left) && !Player.GetGlobalBounds().Intersects(Net.GetGlobalBounds())) Player.Move(new Vector2f(-Player.Speed, 0));
- if (Keyboard.IsKeyPressed(Keyboard.Key.Right) && Player.Position.X + Player.Size.X /2 < 1000) Player.Move(new Vector2f(Player.Speed, 0));
- // ############################# PLAYER 2 PORUSZANIE #######################
- if (Player2.Position.Y < Player2.StartingPosition.Y)
- {
- float FallSpeed = 2;
- Player2.MovementVector = Player2.MovementVector + new Vector2f(0, 600) * DeltaT * FallSpeed;
- // jesli player jest na ziemi, upewnij sie zeby nie wypadl za ekran
- if ((Player2.Position.Y + Player2.MovementVector.Y * DeltaT) >= Player2.StartingPosition.Y)
- {
- Player2.Position = new Vector2f(Player2.Position.X, Player2.StartingPosition.Y);
- Player2.MovementVector = new Vector2f(0, 0);
- }
- }
- if (Player2.Position.Y > Player2.StartingPosition.Y) // po punkcie
- {
- Player2.Position = new Vector2f(Player2.Position.X, Player2.StartingPosition.Y);
- Player2.MovementVector = new Vector2f(0, 0);
- }
- // skakanie
- else if (Player2.Position.Y == Player2.StartingPosition.Y)
- {
- if (Keyboard.IsKeyPressed(Keyboard.Key.W) && WhoIsActualPlayer == 0) Player2.MovementVector = new Vector2f(0, -700);
- else if (WhoIsActualPlayer != 0 && Ball.Position.X > 100) Player2.MovementVector = new Vector2f(0, -700);
- }
- // sterowanie lewo prawo
- if (Keyboard.IsKeyPressed(Keyboard.Key.D) && !Player2.GetGlobalBounds().Intersects(Net.GetGlobalBounds()) && WhoIsActualPlayer == 0) Player2.Move(new Vector2f(Player2.Speed, 0));
- if (Keyboard.IsKeyPressed(Keyboard.Key.A) && Player2.Position.X - Player2.Size.X / 2 > 0 && WhoIsActualPlayer == 0 ) Player2.Move(new Vector2f(-Player2.Speed, 0));
- // ############################# BOTY PORUSZANIE #######################
- if (WhoIsActualPlayer != 0)
- {
- if (Ball.Position.X < 490 && Player2.Position.X + Player2.Size.X /2 < 500 && Player2.Position.X - Player2.Size.X/2 > 0)
- {
- if (Player2.Position.X + Player2.Size.X /2 < Ball.Position.X - 15)
- {
- Player2.Move(Player2.Speed, 0);
- }
- else if (Player2.Position.X - Player2.Size.X /2 > Ball.Position.X - 15)
- {
- Player2.Move(-Player2.Speed, 0);
- }
- else if (Math.Abs(Player2.Position.X - Ball.Position.X) < 15)
- {
- Player2.Move(-Player2.Speed, 0);
- }
- }
- }
- // ############# PUNKTY #############
- if (Player2.Score >= MaxPoints && Player2.Score > Player.Score + 1 )
- {
- gameState = GameState.Menu;
- GameOver = true;
- RightIsWinner = false;
- }
- if (Player.Score >= MaxPoints && Player.Score > Player2.Score + 1)
- {
- gameState = GameState.Menu;
- GameOver = true;
- RightIsWinner = true;
- }
- if (GameOver == true)// koniec gry
- {
- if (!RightIsWinner && WhoIsActualPlayer != 0) LosingGame.Play();
- else WinningGame.Play();
- if (RightIsWinner == true) // gracz po prawej
- {
- Player.Position = new Vector2f (790, 123);
- Player2.Position = new Vector2f(192, 202);
- Player.ResetScore();
- Player2.ResetScore();
- FirstTouch = false;
- Ball.BallReset();
- }
- else // gracz po lewej
- {
- Player2.Position = new Vector2f(790, 123);
- Player.Position = new Vector2f(192, 202);
- Player.ResetScore();
- Player2.ResetScore();
- FirstTouch = false;
- Ball.BallReset();
- }
- }
- if (Keyboard.IsKeyPressed(Keyboard.Key.U)) Player.AddScore(3);
- if (Keyboard.IsKeyPressed(Keyboard.Key.Y))
- {
- Player2.AddScore(3);
- AI1.AddScore(3);
- AI2.AddScore(3);
- AI3.AddScore(3);
- }
- // ############################## PIŁKA ###############################
- // znacznik pilki poza ekranem
- if (Ball.Position.Y + Ball.BallRadius < 0)
- {
- IsBallOnScreen = false;
- BallHelp.Size = new Vector2f(-1500 / (Ball.Position.Y), -1500 / (Ball.Position.Y));
- BallHelp.Origin = new Vector2f(BallHelp.Size.X / 2, 0);
- BallHelp.Position = new Vector2f(Ball.Position.X, 5);
- }
- else if (Ball.Position.Y + Ball.BallRadius > 0) IsBallOnScreen = true;
- // grawitacja pilki
- if (Ball.Position.Y + Ball.BallRadius < 600 && FirstTouch == true)
- {
- float BallFallSpeed = 1;
- Ball.MovementVector = Ball.MovementVector + new Vector2f(0, 500) * DeltaT * BallFallSpeed;
- }
- // pilka dotyka ziemi
- if (Ball.Position.Y + Ball.BallRadius >= 600)
- {
- if (Ball.Position.X < 500)
- {
- FirstTouch = false;
- Ball.MovementVector = new Vector2f(0, 0);
- Ball.Position = BallLeftPosition;
- Player.HitCounter = 0;
- Player2.HitCounter = 0;
- Player2.AddScore(1); // punkty
- Player.Position = Player.StartingPosition;
- Player2.Position = Player2.StartingPosition;
- Ball.DzwiekOdbicia(RandomNumber);
- }
- else if (Ball.Position.X > 500)
- {
- FirstTouch = false;
- Ball.MovementVector = new Vector2f(0, 0);
- Ball.Position = BallRightPosition;
- Player.HitCounter = 0;
- Player2.HitCounter = 0;
- Player.AddScore(1); // punkty
- Player.Position = Player.StartingPosition;
- Player2.Position = Player2.StartingPosition;
- Ball.DzwiekOdbicia(RandomNumber);
- }
- }
- // kolizja pilka <-> sciana
- if (Ball.Position.X + Ball.BallRadius >= 1000)
- {
- Ball.MovementVector = new Vector2f(-Ball.MovementVector.X, Ball.MovementVector.Y);
- while (Ball.Position.X + Ball.BallRadius > 1000)
- {
- Ball.Move(-2, 0);
- }
- }
- if (Ball.Position.X - Ball.BallRadius <= 0)
- {
- Ball.MovementVector = new Vector2f(-Ball.MovementVector.X, Ball.MovementVector.Y);
- while (Ball.Position.X - Ball.BallRadius < 0)
- {
- Ball.Move(2, 0);
- }
- }
- // kolizja pilka <-> siatka
- if (Ball.Position.Y < Net.Position.Y - 2 && (Math.Sqrt((Math.Pow(Net.Position.X - Ball.Position.X, 2) + Math.Pow(Net.Position.Y - Ball.Position.Y, 2)))) <= Ball.BallRadius + Net.Size.X /2 && Ball.MovementVector.Y > 0 )
- {
- while ((Math.Sqrt((Math.Pow(Net.Position.X - Ball.Position.X, 2) + Math.Pow(Net.Position.Y - Ball.Position.Y, 2)))) <= Ball.BallRadius + Net.Size.X / 2 && Ball.MovementVector.Y > 0)
- {
- Ball.Move(((Ball.Position.X - Net.Position.X) * 1 / 20), ((Ball.Position.Y - Net.Position.Y) * 1 / 20));
- }
- Ball.MovementVector = new Vector2f((Ball.Position.X - Net.Position.X) * 3, -Ball.MovementVector.Y);
- Net.DzwiekOdbicia(RandomNumber);
- }
- if (Ball.Position.Y >= Net.Position.Y && Ball.GetGlobalBounds().Intersects(Net.GetGlobalBounds()))
- {
- while (Ball.Position.X - Ball.BallRadius < Net.Position.X + Net.Size.X / 2 && Ball.Position.X + Ball.BallRadius >Net.Position.X + Net.Size.X / 2 )
- {
- Ball.Move(1, 0);
- }
- while (Ball.Position.X + Ball.BallRadius > Net.Position.X - Net.Size.X / 2 && Ball.Position.X - Ball.BallRadius < Net.Position.X - Net.Size.X / 2)
- {
- Ball.Move(-1, 0);
- }
- Ball.MovementVector = new Vector2f(-Ball.MovementVector.X, Ball.MovementVector.Y);
- Net.DzwiekOdbicia(RandomNumber);
- }
- // ########## PILKA <-> PLAYER 1 #####################
- // gorna czesc gracza
- if ((Ball.Position.Y < Player.Position.Y - 10) && (Math.Sqrt((Math.Pow(Player.Position.X - Ball.Position.X, 2) + Math.Pow(Player.Position.Y - Ball.Position.Y, 2)))) <= Ball.BallRadius + (Player.Size.X) / 2 && Player.HitCounter != 3)
- {
- FirstTouch = true;
- while ((Math.Sqrt((Math.Pow(Player.Position.X - Ball.Position.X, 2) + Math.Pow(Player.Position.Y - Ball.Position.Y, 2)))) <= Ball.BallRadius + (Player.Size.X) / 2)
- {
- Ball.Move((Ball.Position.X - Player.Position.X) * 1 / 20, (Ball.Position.Y - Player.Position.Y) * 1 / 20);
- }
- if (Player.MovementVector.Y < -600 && Player.MovementVector.Y != 0)// gracz nie spada za szubko
- {
- Ball.MovementVector = new Vector2f((Ball.Position.X - Player.Position.X) * Player.Strenght, (Player.MovementVector.Y + Ball.Position.Y - Player.Position.Y));
- }
- else if (Player.MovementVector.Y == 0)
- {
- Ball.MovementVector = new Vector2f((Ball.Position.X - Player.Position.X) * Player.Strenght, (Player.MovementVector.Y + Ball.Position.Y - Player.Position.Y) * Player.Strenght);
- }
- else Ball.MovementVector = new Vector2f((Ball.Position.X - Player.Position.X) * Player.Strenght, (Ball.Position.Y - Player.Position.Y) * 6);
- Player2.HitCounter = 0;
- Player.HitCounter += 1;
- Player.DzwiekOdbicia(RandomNumber);
- }
- // dolna czesc gracza
- if ((Ball.Position.Y >= Player.Position.Y - 10) && Ball.GetGlobalBounds().Intersects(Player.GetGlobalBounds()) && Player.HitCounter != 3)
- {
- FirstTouch = true; // serwis
- if (Math.Abs(Ball.MovementVector.X) < 200) // pilka nie leci zbyt wolno
- {
- if (Ball.Position.X < Player.Position.X) Ball.MovementVector = new Vector2f(-200, Ball.MovementVector.Y);
- else Ball.MovementVector = new Vector2f(200, Ball.MovementVector.Y);
- }
- else Ball.MovementVector = new Vector2f(-(Ball.MovementVector.X), Ball.MovementVector.Y);
- while (Ball.GetGlobalBounds().Intersects(Player.GetGlobalBounds()))
- {
- Ball.Move((Ball.Position.X - Player.Position.X) * 1 / 20, (Ball.Position.Y - Player.Position.Y) * 1 / 20);
- }
- Player2.HitCounter = 0;
- Player.HitCounter += 1;
- Player.DzwiekOdbicia(RandomNumber);
- }
- Console.WriteLine(Ball.MovementVector.Y);
- // ########## PILKA <-> PLAYER 2 #####################
- // gorna czesc gracza
- if ((Ball.Position.Y < Player2.Position.Y - 10) && (Math.Sqrt((Math.Pow(Player2.Position.X - Ball.Position.X, 2) + Math.Pow(Player2.Position.Y - Ball.Position.Y, 2)))) <= Ball.BallRadius + (Player2.Size.X) / 2 && Player2.HitCounter != 3)
- {
- FirstTouch = true;
- while ((Math.Sqrt((Math.Pow(Player2.Position.X - Ball.Position.X, 2) + Math.Pow(Player2.Position.Y - Ball.Position.Y, 2)))) <= Ball.BallRadius + (Player2.Size.X) / 2)
- {
- Ball.Move((Ball.Position.X - Player2.Position.X) * 1 / 20, (Ball.Position.Y - Player2.Position.Y) * 1 / 20);
- }
- if (Player2.MovementVector.Y < -600 && Player2.MovementVector.Y != 0)
- {
- Ball.MovementVector = new Vector2f((Ball.Position.X - Player2.Position.X) * Player2.Strenght, (Player2.MovementVector.Y + Ball.Position.Y - Player2.Position.Y));
- }
- else if (Player2.MovementVector.Y == 0)
- {
- Ball.MovementVector = new Vector2f((Ball.Position.X - Player2.Position.X) * Player2.Strenght, (Player2.MovementVector.Y + Ball.Position.Y - Player2.Position.Y) * 0.66F * Player2.Strenght);
- }
- else Ball.MovementVector = new Vector2f((Ball.Position.X - Player2.Position.X) * Player2.Strenght, (Ball.Position.Y - Player2.Position.Y) * Player2.Strenght);
- Player.HitCounter = 0;
- Player2.HitCounter += 1;
- Player2.DzwiekOdbicia(RandomNumber);
- }
- // dolna czesc gracza
- if ((Ball.Position.Y >= Player2.Position.Y - 10) && Ball.GetGlobalBounds().Intersects(Player2.GetGlobalBounds()) && Player2.HitCounter != 3)
- {
- FirstTouch = true; // serwis
- if (Math.Abs(Ball.MovementVector.X) < 200) // pilka nie leci zbyt wolno
- {
- if (Ball.Position.X < Player2.Position.X) Ball.MovementVector = new Vector2f(-200, Ball.MovementVector.Y);
- else Ball.MovementVector = new Vector2f(200, Ball.MovementVector.Y);
- }
- else Ball.MovementVector = new Vector2f(-(Ball.MovementVector.X), Ball.MovementVector.Y);
- while (Ball.GetGlobalBounds().Intersects(Player2.GetGlobalBounds()))
- {
- Ball.Move((Ball.Position.X - Player2.Position.X) * 1 / 20, (Ball.Position.Y - Player2.Position.Y) * 1 / 20);
- }
- Player.HitCounter = 0;
- Player2.HitCounter += 1;
- Player.DzwiekOdbicia(RandomNumber);
- }
- // ############################### PORUSZANIE WSZYSTKIM #################
- Player.Move(Player.MovementVector * DeltaT);
- Player2.Move(Player2.MovementVector * DeltaT);
- Ball.Move(Ball.MovementVector * DeltaT * 0.8F);
- if (Keyboard.IsKeyPressed(Keyboard.Key.M))
- {
- gameState = GameState.Menu;
- }
- }
- }
- public void DisplayMenu()
- {
- // ############################ PODPINANIE TEKSTUR ###########################
- MenuBackground.Texture = MenuBackgroundTex;
- GraczVsGracz.Texture = GraczVsGraczTex;
- GraczVsKomputer.Texture = GraczVsKomputerTex;
- ExitButton.Texture = ExitButtonTex;
- Button1.Texture = Button1Tex;
- Button2.Texture = Button2Tex;
- Button3.Texture = Button3Tex;
- LevelButton.Texture = LevelButtonTex;
- Points15Button.Texture = Points15ButtonTex;
- Points20Button.Texture = Points20ButtonTex;
- Points25Button.Texture = Points25ButtonTex;
- PointsButton.Texture = PointsButtonTex;
- // ############################### SKALA I POZYCJA ##############################
- GraczVsGracz.Position = new Vector2f(20, 260);
- GraczVsGracz.Scale = new Vector2f(0.6F, 0.6F);
- GraczVsKomputer.Position = new Vector2f(20, 330);
- GraczVsKomputer.Scale = new Vector2f(0.6F, 0.6F);
- ExitButton.Position = new Vector2f(5, 545);
- ExitButton.Scale = new Vector2f(0.3F, 0.3F);
- LevelButton.Position = new Vector2f(20, 330);
- LevelButton.Scale = new Vector2f(0.6F, 0.6F);
- PointsButton.Position = new Vector2f(20, 400);
- PointsButton.Scale = new Vector2f(0.6F, 0.6F);
- Points15Button.Position = new Vector2f(20, 470);
- Points15Button.Scale = new Vector2f(0.6F, 0.6F);
- Points20Button.Position = new Vector2f(140, 470);
- Points20Button.Scale = new Vector2f(0.6F, 0.6F);
- Points25Button.Position = new Vector2f(260, 470);
- Points25Button.Scale = new Vector2f(0.6F, 0.6F);
- Button1.Position = new Vector2f(20, 400);
- Button1.Scale = new Vector2f(0.6F, 0.6F);
- Button2.Position = new Vector2f(140, 400);
- Button2.Scale = new Vector2f(0.6F, 0.6F);
- Button3.Position = new Vector2f(260, 400);
- Button3.Scale = new Vector2f(0.6F, 0.6F);
- // ############################## INNE #####################################
- bool DrawPoints = false;
- bool DrawLevel = false;
- Clock C = new Clock();
- float _T = T.AsSeconds();
- while (true)
- {
- // zamykanie okna
- Window.DispatchEvents();
- ProcEvents();
- // gracz vs gracz
- if (Mouse.IsButtonPressed(Mouse.Button.Left) && GraczVsGracz.GetGlobalBounds().Contains(Mouse.GetPosition(Window).X, Mouse.GetPosition(Window).Y) || Keyboard.IsKeyPressed(Keyboard.Key.R))
- {
- WhoIsActualPlayer = 0;
- Player2 = NotAI;
- Player.ResetScore();
- FirstTouch = false;
- Ball.BallReset();
- Player.Position = Player.StartingPosition;
- Player.HitCounter = 0;
- gameState = GameState.VsPlayer;
- }
- if (Mouse.IsButtonPressed(Mouse.Button.Left) && GraczVsKomputer.GetGlobalBounds().Contains(Mouse.GetPosition(Window).X, Mouse.GetPosition(Window).Y))
- {
- DrawLevel = true;
- DrawPoints = false;
- }
- if (Mouse.IsButtonPressed(Mouse.Button.Left) && Button1.GetGlobalBounds().Contains(Mouse.GetPosition(Window).X, Mouse.GetPosition(Window).Y) && DrawLevel)
- {
- WhoIsActualPlayer = 1;
- Player2 = AI1;
- Player.ResetScore();
- FirstTouch = false;
- Ball.BallReset();
- Player.Position = Player.StartingPosition;
- Player.HitCounter = 0;
- Player2.Position = Player2.StartingPosition;
- Player2.HitCounter = 0;
- gameState = GameState.VsAI;
- }
- if (Mouse.IsButtonPressed(Mouse.Button.Left) && Button2.GetGlobalBounds().Contains(Mouse.GetPosition(Window).X, Mouse.GetPosition(Window).Y) && DrawLevel)
- {
- WhoIsActualPlayer = 2;
- Player2 = AI2;
- Player.ResetScore();
- FirstTouch = false;
- Ball.BallReset();
- Player.Position = Player.StartingPosition;
- Player.HitCounter = 0;
- Player2.Position = Player2.StartingPosition;
- Player2.HitCounter = 0;
- gameState = GameState.VsAI;
- }
- if (Mouse.IsButtonPressed(Mouse.Button.Left) && Button3.GetGlobalBounds().Contains(Mouse.GetPosition(Window).X, Mouse.GetPosition(Window).Y) && DrawLevel)
- {
- WhoIsActualPlayer = 3;
- Player2 = AI3;
- Player.ResetScore();
- FirstTouch = false;
- Ball.BallReset();
- Player.Position = Player.StartingPosition;
- Player.HitCounter = 0;
- Player2.Position = Player2.StartingPosition;
- Player2.HitCounter = 0;
- gameState = GameState.VsAI;
- }
- if (Mouse.IsButtonPressed(Mouse.Button.Left) && PointsButton.GetGlobalBounds().Contains(Mouse.GetPosition(Window).X, Mouse.GetPosition(Window).Y) && !DrawLevel)
- {
- DrawPoints = true;
- }
- if (Mouse.IsButtonPressed(Mouse.Button.Left) && Points15Button.GetGlobalBounds().Contains(Mouse.GetPosition(Window).X, Mouse.GetPosition(Window).Y) && !DrawLevel)
- {
- MaxPoints = 15;
- gameState = GameState.Menu;
- DrawPoints = false;
- }
- if (Mouse.IsButtonPressed(Mouse.Button.Left) && Points20Button.GetGlobalBounds().Contains(Mouse.GetPosition(Window).X, Mouse.GetPosition(Window).Y) && !DrawLevel)
- {
- MaxPoints = 20;
- gameState = GameState.Menu;
- DrawPoints = false;
- }
- if (Mouse.IsButtonPressed(Mouse.Button.Left) && Points25Button.GetGlobalBounds().Contains(Mouse.GetPosition(Window).X, Mouse.GetPosition(Window).Y))
- {
- MaxPoints = 25;
- gameState = GameState.Menu;
- DrawPoints = false;
- }
- if ((Mouse.IsButtonPressed(Mouse.Button.Left) && ExitButton.GetGlobalBounds().Contains(Mouse.GetPosition(Window).X, Mouse.GetPosition(Window).Y)) || Keyboard.IsKeyPressed(Keyboard.Key.Escape))
- {
- gameState = GameState.Closing;
- }
- if (Keyboard.IsKeyPressed(Keyboard.Key.Return))
- {
- GameOver = false;
- gameState = GameState.Menu;
- WinningGame.Stop();
- LosingGame.Stop();
- }
- if (gameState != GameState.Menu)
- {
- return;
- }
- Window.Clear();
- // rysowanie menu
- Window.Draw(MenuBackground);
- Window.Draw(GraczVsGracz);
- Window.Draw(GraczVsKomputer);
- Window.Draw(ExitButton);
- //Window.Draw(PlayerSprite);
- if (!DrawLevel) Window.Draw(PointsButton);
- if (DrawLevel)
- {
- Window.Draw(LevelButton);
- Window.Draw(Button1);
- Window.Draw(Button2);
- Window.Draw(Button3);
- }
- if (DrawPoints)
- {
- Window.Draw(Points15Button);
- Window.Draw(Points20Button);
- Window.Draw(Points25Button);
- }
- if (GameOver == true)
- {
- Window.Draw(EndGame);
- Window.Draw(Player);
- Window.Draw(Player2);
- }
- Window.Display();
- while (C.ElapsedTime < T) ;
- C.Restart();
- }
- }
- public void Render()
- {
- Window.Clear();
- Window.Draw(Background);
- Window.Draw(Player);
- Window.Draw(Player2);
- Window.Draw(Ball);
- Window.Draw(Net);
- if (!IsBallOnScreen) Window.Draw(BallHelp);
- Window.Draw(ScoreText);
- Window.Display();
- }
- public void RunGame()
- {
- Time TimeSinceLastUpdate = Time.Zero;
- Time TimePerFrame = Time.FromSeconds(TICK);
- Clock Clock = new Clock();
- while (Window.IsOpen)
- {
- TimeSinceLastUpdate += Clock.Restart();
- while (TimeSinceLastUpdate > TimePerFrame)
- {
- TimeSinceLastUpdate -= TimePerFrame;
- Window.DispatchEvents();
- Update(TimePerFrame);
- if (gameState != GameState.VsPlayer || gameState!= GameState.VsAI)
- {
- return;
- }
- }
- Render();
- ProcEvents();
- }
- }
- public void Run()
- {
- GameMusic.Play();
- while (true)
- {
- switch (gameState)
- {
- case GameState.Menu:
- DisplayMenu();
- break;
- case GameState.VsAI:
- RunGame();
- break;
- case GameState.VsPlayer:
- RunGame();
- break;
- case GameState.Closing:
- Window.Close();
- return;
- case GameState.WinningScreen:
- break;
- }
- }
- }
- }
- }
- BALL
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using SFML.Audio;
- using SFML.Graphics;
- using SFML.System;
- using SFML.Window;
- namespace Siatkowkaa2
- {
- class Ball : Entity
- {
- public float BallRadius { get; set; }
- private SoundBuffer GroundHitBuf1 = new SoundBuffer(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\groundHit1.wav");
- private SoundBuffer GroundHitBuf2 = new SoundBuffer(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\groundHit2.wav");
- private SoundBuffer GroundHitBuf3 = new SoundBuffer(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\point.wav");
- private Sound GroundHit1 = new Sound();
- private Sound GroundHit2 = new Sound();
- private Sound GroundHit3 = new Sound();
- public void BallReset()
- {
- this.MovementVector = new Vector2f(0, 0);
- this.Position = new Vector2f(750, 200);
- }
- public Ball(float ballradius, Vector2f position)
- {
- Size = new Vector2f(ballradius, ballradius);
- Origin = new Vector2f(ballradius / 2, ballradius / 2);
- Position = position;
- BallRadius = ballradius/2;
- GroundHit1.SoundBuffer = GroundHitBuf1;
- GroundHit2.SoundBuffer = GroundHitBuf2;
- GroundHit3.SoundBuffer = GroundHitBuf3;
- GroundHit1.Volume = 20;
- GroundHit2.Volume = 20;
- GroundHit3.Volume = 20;
- }
- override public void DzwiekOdbicia(int random)
- {
- if (random < 4) GroundHit1.Play();
- if (random > 4) GroundHit3.Play();
- else if (random == 4) GroundHit2.Play();
- }
- }
- }
- PLAYER
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using SFML.Audio;
- using SFML.Graphics;
- using SFML.System;
- using SFML.Window;
- namespace Siatkowkaa2
- {
- class Player : Entity
- {
- public Vector2f StartingPosition { get; set; }
- public RectangleShape Shape;
- public int Score { get; private set; } = 0;
- public float Speed { get; set; }
- public int HitCounter { get; set; } = 0;
- public int Strenght { get; set; }
- private SoundBuffer BallHitBuf1 = new SoundBuffer(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\hit1.wav");
- private SoundBuffer BallHitBuf2 = new SoundBuffer(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\hit2.wav");
- private SoundBuffer BallHitBuf3 = new SoundBuffer(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\hit3.wav");
- private SoundBuffer BallHitBuf4 = new SoundBuffer(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\hit4.wav");
- private SoundBuffer BallHitBuf5 = new SoundBuffer(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\hit5.wav");
- private SoundBuffer BallHitBuf6 = new SoundBuffer(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\hit6.wav");
- private SoundBuffer BallHitBuf7 = new SoundBuffer(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\hit7.wav");
- private SoundBuffer BallHitBuf8 = new SoundBuffer(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\hit8.wav");
- private SoundBuffer BallHitBuf9 = new SoundBuffer(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\hit9.wav");
- private Sound BallHit1 = new Sound();
- private Sound BallHit2 = new Sound();
- private Sound BallHit3 = new Sound();
- private Sound BallHit4 = new Sound();
- private Sound BallHit5 = new Sound();
- private Sound BallHit6 = new Sound();
- private Sound BallHit7 = new Sound();
- private Sound BallHit8 = new Sound();
- private Sound BallHit9 = new Sound();
- public void AddScore(int Amount)
- {
- Score += Amount;
- }
- public Player(Vector2f position, Vector2f size, float speed, int strenght)
- {
- this.Size = size;
- this.Origin = new Vector2f(size.X / 2, size.X / 2);
- this.Position = position;
- this.StartingPosition = position;
- this.Speed = speed;
- Strenght = strenght;
- BallHit1.SoundBuffer = BallHitBuf1;
- BallHit2.SoundBuffer = BallHitBuf2;
- BallHit3.SoundBuffer = BallHitBuf3;
- BallHit4.SoundBuffer = BallHitBuf4;
- BallHit5.SoundBuffer = BallHitBuf5;
- BallHit6.SoundBuffer = BallHitBuf6;
- BallHit7.SoundBuffer = BallHitBuf7;
- BallHit8.SoundBuffer = BallHitBuf8;
- BallHit9.SoundBuffer = BallHitBuf9;
- BallHit1.Volume = 30;
- BallHit2.Volume = 30;
- BallHit3.Volume = 30;
- BallHit4.Volume = 30;
- BallHit5.Volume = 30;
- BallHit6.Volume = 30;
- BallHit7.Volume = 30;
- BallHit8.Volume = 30;
- BallHit9.Volume = 30;
- }
- public void ResetScore()
- {
- Score = 0;
- }
- override public void DzwiekOdbicia(int random)
- {
- switch (random)
- {
- case 1:
- BallHit1.Play();
- break;
- case 2:
- BallHit2.Play();
- break;
- case 3:
- BallHit3.Play();
- break;
- case 4:
- BallHit4.Play();
- break;
- case 5:
- BallHit5.Play();
- break;
- case 6:
- BallHit6.Play();
- break;
- case 7:
- BallHit7.Play();
- break;
- case 8:
- BallHit8.Play();
- break;
- case 9:
- BallHit9.Play();
- break;
- }
- }
- }
- }
- NET
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using SFML.Audio;
- using SFML.Graphics;
- using SFML.System;
- using SFML.Window;
- namespace Siatkowkaa2
- {
- class Net : Entity
- {
- RectangleShape Shape;
- private SoundBuffer NetHitBuf1 = new SoundBuffer(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\nethit.wav");
- private SoundBuffer NetHitBuf2 = new SoundBuffer(@"F:\Visual\Siatkowkaa2\Siatkowkaa2\Resources\Sounds\nethit2.wav");
- private Sound NetHit1 = new Sound();
- private Sound NetHit2 = new Sound();
- public Net(Vector2f size)
- {
- Size = size;
- Origin = new Vector2f(size.X / 2, size.X / 2);
- Position = new Vector2f(500, 600 - size.Y + size.X);
- NetHit1.SoundBuffer = NetHitBuf1;
- NetHit2.SoundBuffer = NetHitBuf2;
- NetHit1.Volume = 20;
- NetHit2.Volume = 20;
- }
- override public void DzwiekOdbicia(int random)
- {
- if (random < 5) NetHit1.Play();
- else NetHit2.Play();
- }
- }
- }
- ENTITY
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using SFML.Audio;
- using SFML.Graphics;
- using SFML.System;
- using SFML.Window;
- namespace Siatkowkaa2
- {
- class Entity : RectangleShape
- {
- public Vector2f MovementVector {get; set; }
- public virtual void DzwiekOdbicia(int random)
- { }
- public void Move(Vector2f offset)
- {
- Position += offset;
- }
- public void Move(float offsetX, float offsetY)
- {
- Position += new Vector2f(offsetX, offsetY);
- }
- public Entity()
- {
- MovementVector = new Vector2f(0.0f, 0.0f);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement