Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using FE;
- using UnityEngine;
- public class Pong: Game
- {
- private enum PongStates
- {
- TitleScreen,
- Game,
- Win,
- GameOver
- }
- private FuseEngine fe;
- public int playerAScore = 0;
- public int playerBScore = 0;
- int scoreToWin = 5;
- public int centerX = 0;
- public int centerY = 0;
- public PongPaddle paddleA;
- public PongPaddle paddleB;
- public float ballX = 0;
- public float ballY = 0;
- float ballVX = 0;
- float ballVY = 0;
- public int ballSize = 4;
- int ballSpeed = 40;
- bool ballServed = false;
- int cursorX, cursorY = 0;
- private PongStates pongState = PongStates.TitleScreen;
- bool isReady = false;
- CPU cpu;
- public Pong(FuseEngine _fe, CPU _cpu)
- {
- fe = _fe;
- cpu = _cpu;
- fe.setFrameRate(60);
- centerY = fe.height / 2;
- centerX = fe.width / 2;
- cursorX = centerX;
- cursorY = centerY;
- ballReset();
- fe.clear();
- fe.display();
- paddleA = new PongPaddle(fe, this);
- paddleB = new PongPaddle(fe, this);
- paddleA.setPosition(new Vector2Int(5, centerY - (paddleA.paddleHeight / 2)));
- paddleB.setPosition(new Vector2Int(fe.width - paddleB.paddleWidth - 5, centerY - (paddleB.paddleHeight / 2)));
- isReady = true;
- }
- // Update is called once per frame
- public override void Update()
- {
- if (isReady)
- {
- if (!fe.nextFrame()) return;
- fe.clear();
- handleStates();
- // REQUIRED for Input //
- handleInput();
- fe.display();
- }
- }
- private void handleStates()
- {
- switch(pongState)
- {
- case PongStates.TitleScreen:
- titleScreen();
- break;
- case PongStates.Game:
- gameScreen();
- break;
- case PongStates.Win:
- GameWinScreen();
- break;
- case PongStates.GameOver:
- GameLoseScreen();
- break;
- }
- }
- public void cursorMovement()
- {
- cursorX += Controls.Horizontal * 2;
- cursorY -= Controls.Vertical * 2;
- if (cursorX <= 0)
- {
- cursorX = 0;
- }
- if (cursorX >= fe.width - 1)
- {
- cursorX = fe.width - 1;
- }
- if (cursorY >= 144)
- {
- cursorY = 144;
- }
- if (cursorY <= 0)
- {
- cursorY = 0;
- }
- drawCursor();
- }
- public void drawCursor()
- {
- int cX = cursorX;
- int cY = cursorY;
- fe.drawFastHLine(cX, cY + 1, 2, Color.blue);
- fe.drawFastHLine(cX, cY - 1, 2, Color.blue);
- fe.drawFastVLine(cX + 1, cY, 2, Color.blue);
- fe.drawFastVLine(cX - 1, cY - 1, 3, Color.blue);
- }
- private void titleScreen()
- {
- drawTitleText();
- }
- private void gameScreen()
- {
- field();
- drawBall();
- paddleA.drawPaddle();
- paddleB.drawPaddle();
- ballPaddleCollision(paddleA);
- ballPaddleCollision(paddleB);
- paddleControls();
- }
- private void GameWinScreen()
- {
- int ngPY = 64;
- int gymOvr = 90;
- Point cursorPoint = new Point(cursorX, cursorY);
- cursorMovement();
- Rect NGRect = new Rect(30, ngPY, 100, 20);
- Rect QURect = new Rect(30, gymOvr, 100, 20);
- fe.writeString(35, 25, "You Win!", 2);
- fe.writeString((int)NGRect.x + 2, ngPY + 2, "New Game", 2);
- fe.writeString((int)QURect.x + 2, gymOvr + 3, "Quit", 2);
- if (fe.collide(cursorPoint, NGRect))
- {
- if (fe.everyXFrames(3))
- {
- fe.drawRect((int)NGRect.x, (int)NGRect.y - 2, (int)NGRect.width, (int)NGRect.height, Color.blue);
- }
- if (Controls.buttonADown)
- {
- resetGame();
- }
- }
- else
- {
- fe.drawRect((int)NGRect.x, (int)NGRect.y - 2, (int)NGRect.width, (int)NGRect.height, Color.blue);
- }
- if (fe.collide(cursorPoint, QURect))
- {
- if (fe.everyXFrames(3))
- {
- fe.drawRect((int)QURect.x, (int)QURect.y, (int)QURect.width, (int)QURect.height, Color.blue);
- }
- if (Controls.buttonADown)
- {
- isReady = false;
- quitGame();
- }
- }
- else
- {
- fe.drawRect((int)QURect.x, (int)QURect.y, (int)QURect.width, (int)QURect.height, Color.blue);
- }
- }
- private void resetGame()
- {
- ballReset();
- pongState = PongStates.Game;
- playerAScore = 0;
- playerBScore = 0;
- }
- private void drawTitleText()
- {
- int ngPY = 64;
- int gymOvr = 90;
- Point cursorPoint = new Point(cursorX, cursorY);
- Rect NGRect = new Rect(30, ngPY, 100, 20);
- Rect QURect = new Rect(30, gymOvr, 100, 20);
- cursorMovement();
- fe.writeString(28, 20, "Bead Ball", 2);
- fe.writeString((int)NGRect.x + 2, ngPY + 2, "New Game", 2);
- fe.writeString((int)QURect.x + 2, gymOvr + 3, "Quit", 2);
- if (fe.collide(cursorPoint, NGRect))
- {
- if (fe.everyXFrames(3))
- {
- fe.drawRect((int)NGRect.x, (int)NGRect.y - 2, (int)NGRect.width, (int)NGRect.height, Color.blue);
- }
- if (Controls.buttonADown)
- {
- resetGame();
- }
- }
- else
- {
- fe.drawRect((int)NGRect.x, (int)NGRect.y - 2, (int)NGRect.width, (int)NGRect.height, Color.blue);
- }
- if (fe.collide(cursorPoint, QURect))
- {
- if (fe.everyXFrames(3))
- {
- fe.drawRect((int)QURect.x, (int)QURect.y, (int)QURect.width, (int)QURect.height, Color.blue);
- }
- if (Controls.buttonADown)
- {
- isReady = false;
- quitGame();
- }
- }
- else
- {
- fe.drawRect((int)QURect.x, (int)QURect.y, (int)QURect.width, (int)QURect.height, Color.blue);
- }
- }
- private void GameLoseScreen()
- {
- int ngPY = 64;
- int gymOvr = 90;
- Point cursorPoint = new Point(cursorX, cursorY);
- cursorMovement();
- Rect NGRect = new Rect(30, ngPY, 100, 20);
- Rect QURect = new Rect(30, gymOvr, 100, 20);
- fe.writeString(29, 25, "Game Over", 2);
- fe.writeString((int)NGRect.x + 2, ngPY + 2, "New Game", 2);
- fe.writeString((int)QURect.x + 2, gymOvr + 3, "Quit", 2);
- if (fe.collide(cursorPoint, NGRect))
- {
- if (fe.everyXFrames(3))
- {
- fe.drawRect((int)NGRect.x, (int)NGRect.y - 2, (int)NGRect.width, (int)NGRect.height, Color.blue);
- }
- if (Controls.buttonADown)
- {
- resetGame();
- }
- }
- else
- {
- fe.drawRect((int)NGRect.x, (int)NGRect.y - 2, (int)NGRect.width, (int)NGRect.height, Color.blue);
- }
- if (fe.collide(cursorPoint, QURect))
- {
- if (fe.everyXFrames(3))
- {
- fe.drawRect((int)QURect.x, (int)QURect.y, (int)QURect.width, (int)QURect.height, Color.blue);
- }
- if (Controls.buttonADown)
- {
- isReady = false;
- quitGame();
- }
- }
- else
- {
- fe.drawRect((int)QURect.x, (int)QURect.y, (int)QURect.width, (int)QURect.height, Color.blue);
- }
- }
- public void drawBall()
- {
- if (ballServed)
- {
- moveBall();
- } else {
- fe.writeString(30, 30, "Press A to Serve", 1);
- ballReset(0);
- if (Controls.buttonADown)
- {
- ballReset(1);
- ballServed = true;
- }
- }
- int x = Mathf.RoundToInt(ballX);
- int y = Mathf.RoundToInt(ballY);
- fe.drawRect(x, y, ballSize, ballSize, Color.white);
- fe.fillRect(x, y, ballSize, ballSize, Color.white);
- }
- public void ballReset(int direction = 0)
- {
- ballX = centerX;
- ballY = centerY;
- ballVX = direction;
- ballVY = 0;
- }
- public void moveBall()
- {
- if (ballY <= 1 || ballY >= fe.height - ballSize - 1)
- {
- ballVY = -ballVY;
- }
- if (ballX <= 1 || ballX >= fe.width - ballSize - 1)
- {
- if (ballX <= 1)
- {
- playerBScore++;
- }
- if (ballX >= fe.width - ballSize - 1)
- {
- playerAScore++;
- }
- if (playerAScore > scoreToWin)
- {
- pongState = PongStates.Win;
- }
- if (playerBScore > scoreToWin)
- {
- pongState = PongStates.GameOver;
- }
- ballServed = false;
- }
- ballX = ballX + ballVX * ballSpeed * Time.deltaTime;
- ballY = ballY + ballVY * ballSpeed * Time.deltaTime;
- }
- int numDetections = 0;
- public void ballPaddleCollision(PongPaddle paddle)
- {
- float bx = paddle.position.x - ballX - ballSize;
- float by = paddle.position.y - ballY - ballSize;
- int pw = paddle.paddleWidth + ballSize;
- int ph = paddle.paddleHeight + ballSize;
- if (bx <= 0 && bx + pw >= 0 && by <= 0 && by + ph >= 0)
- {
- float pW = paddle.position.x + (paddle.paddleWidth / 2);
- float pH = paddle.position.y + (paddle.paddleHeight / 2);
- Vector2 position = new Vector2(ballX, ballY);
- Vector2 paddleCenter = new Vector2(pW, pH);
- ballVX = 1 - 2 * (paddleCenter.x - position.x) / paddle.paddleWidth;
- ballVY = 1 - 2 * (paddleCenter.y - position.y) / paddle.paddleWidth;
- }
- }
- void paddleControls()
- {
- paddleA.move_implicit(Controls.Vertical);
- paddleB.move_implicit();
- paddleA.update();
- paddleB.update();
- }
- private void field()
- {
- fe.drawRect(0, 0, fe.width - 1, fe.height, Color.blue);
- fe.writeString(centerX - 15, 5, playerAScore.ToString(), 1);
- fe.writeString(centerX + 10, 5, playerBScore.ToString(), 1);
- fe.drawFastVLine(fe.width / 2, 0, fe.height, Color.white);
- fe.drawCircle(centerX, centerY, 14, Color.green);
- }
- public void quitGame()
- {
- fe.clear();
- Unload();
- cpu.reloadSystem();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement