class Main: def __init__(self): pygame.init() #limits the speed of continuous key presses pygame.key.set_repeat(1, 1) random.seed() #initializes variables for game self.width = 800 self.height = 800 self.running = False self.level = "levels/level_test.csv" self.level_map = [] #state of the game self.game_state = "MENU" self.menu_objects = {} self.menu_objects["playButton"] = Button(self.width / 2 - 75, self.height - 255, 150, 50, "Play Game") self.menu_objects["selectLevelButton"] = Button( self.width / 2 - 75, self.height - 200, 150, 50, "Select Level") self.menu_objects["instructionsButton"] = Button( self.width / 2 - 75, self.height - 145, 150, 50, "Instructions") self.menu_objects["backButton"] = Button(5, self.height - 55, 150, 50, "Back") #initializes variables for bricks self.bricks = [] self.initPaddle() self.initBall() self.paddle = Paddle(self.paddleX, self.paddleY, self.paddleWidth, self.paddleHeight, self.paddleVelocity, self.width) self.ball = Ball(self.paddle, self.ballX, self.ballY, self.ballRadius, self.ballVelocityX, self.ballVelocityY, self.width, self.height) #initializes the screen self.screen = pygame.display.set_mode((self.width, self.height)) def initPaddle(self): #initializes variables for paddle self.paddleWidth = 200 self.paddleHeight = 20 self.paddleX = self.width / 2 - self.paddleWidth / 2 self.paddleY = self.height - self.paddleHeight - 10 self.paddleVelocity = 4 def initBall(self): #initializes variables for ball self.ballRadius = 15 self.ballX = self.width / 2 - self.ballRadius self.ballY = self.paddleY - self.ballRadius * 2 self.ballVelocityX = random.randint(4, 7) if random.randint(0, 1) == 0: self.ballVelocityX *= -1 self.ballVelocityY = random.randint(5, 7) * -1 def getInput(self): #loops through all of the events received events = pygame.event.get() for event in events: #quits the game if the 'X' button is clicked if event.type == pygame.QUIT: self.end() #tests which key on the keyboard is clicked if event.type == pygame.KEYDOWN: #updates paddle direction to left if event.key == pygame.K_a or event.key == pygame.K_LEFT: self.paddle.setDirection("left") #updates paddle direction to right if event.key == pygame.K_d or event.key == pygame.K_RIGHT: self.paddle.setDirection("right") #quits the game if escape is clicked if event.key == pygame.K_ESCAPE: self.end() def loadLevel(self): #opens the csv file containing the level file = open(self.level) reader = csv.reader(file) self.level_map = [] #loops through the file and adds each element to an array for row in reader: row_contents = [] for col in row: row_contents.append(col) self.level_map.append(row_contents) #loops through the array and loads the level for i in range(len(self.level_map)): for j in range(len(self.level_map[i])): #creates a new Brick object for every brick in the level if self.level_map[i][j] == 'b': brickX = j * 100 + 5 brickY = i * 50 + 5 brickTier = 1 self.bricks.append(Brick(brickX, brickY, brickTier)) def update(self): #tests which game state the game is in if self.game_state == "PLAYING": #updates the paddle position self.paddle.update() self.ball.update() if self.ball.getRunning() == False: self.game_state = "MENU" self.reset() for brick in self.bricks: if brick.isColliding(self.ball): self.bricks.remove(brick) self.paddle.setDirection("null") elif self.game_state == "MENU": #tests if buttons are clicked if self.menu_objects["playButton"].isClicked(): self.game_state = "PLAYING" elif self.menu_objects["selectLevelButton"].isClicked(): self.game_state = "SELECT LEVEL" elif self.menu_objects["instructionsButton"].isClicked(): self.game_state = "INSTRUCTIONS" elif self.game_state == "INSTRUCTIONS" or self.game_state == "SELECT LEVEL": if self.menu_objects["backButton"].isClicked(): self.game_state = "MENU" def render(self): #creates a background for the game self.screen.fill((0, 0, 0)) #tests which game state the game is in if self.game_state == "PLAYING": #loops through the bricks array and draws each brick to the screen for i in range(len(self.bricks)): pygame.draw.rect( self.screen, (200, 100, 100), (self.bricks[i].getX(), self.bricks[i].getY(), self.bricks[i].getWidth(), self.bricks[i].getHeight())) #draws the paddle to the screen self.paddle.render(self.screen) #draws the ball to the screen self.ball.render(self.screen) elif self.game_state == "MENU": #draws the menu elements to the screen self.menu_objects["playButton"].render(self.screen) self.menu_objects["selectLevelButton"].render(self.screen) self.menu_objects["instructionsButton"].render(self.screen) elif self.game_state == "INSTRUCTIONS": self.menu_objects["backButton"].render(self.screen) elif self.game_state == "SELECT LEVEL": self.menu_objects["backButton"].render(self.screen) #updates the screen pygame.display.update() def reset(self): self.loadLevel() self.initBall() self.initPaddle() self.ball.reset(self.ballX, self.ballY, self.ballVelocityX, self.ballVelocityY) self.paddle.reset(self.paddleX) def main(self): #loads the level and prints contents to the console self.loadLevel() #main game loop while self.running: self.getInput() self.update() self.render() #delays program time.sleep(0.01) #exits the program pygame.quit() sys.exit() def start(self): #starts the game self.running = True self.main() def end(self): #ends the game self.running = False
class PongGame(object): def __init__(self): pygame.init() self.clock = pygame.time.Clock() self.gameDisplay = pygame.display.set_mode( (constant.WIDTH, constant.HEIGHT)) self.text, self.text2, self.font, self.font2, self.textRect, self.text2Rect = None, None, None, None, None, None self.ball, self.paddle1, self.paddle2, self.player1, self.player2 = None, None, None, None, None self.initializeFont() self.initializeObject() self.paddlespeed = 5 self.terminate = False while not self.terminate: self.checkEvent() self.collisionCheck() self.checkScore() self.updateObject() self.updateText() self.renderText() self.renderObject() pygame.display.update() self.clock.tick(120) pygame.quit() def initializeFont(self): self.font = pygame.font.Font("freesansbold.ttf", 32) self.font2 = pygame.font.Font("freesansbold.ttf", 15) self.text = self.font.render("PONG", True, (255, 255, 255)) self.text2 = self.font2.render("press r to reset game", True, (255, 255, 255)) self.textRect = self.text.get_rect() self.text2Rect = self.text2.get_rect() self.text2Rect.center = (constant.WIDTH // 2, 80) self.textRect.center = (constant.WIDTH // 2, 40) def initializeObject(self): self.ball = Ball(constant.WIDTH // 2, constant.HEIGHT // 2, 10, 5, random.uniform(0, math.pi), self.gameDisplay) self.paddle1 = Paddle(30, 20, 150, self.gameDisplay) self.paddle2 = Paddle(constant.WIDTH - 50, 20, 150, self.gameDisplay) self.player1 = Player(self.paddle1) self.player2 = Player(self.paddle2) def resetGame(self): self.ball.reset() self.player1.reset() self.player2.reset() def checkEvent(self): for event in pygame.event.get(): # check events if event.type == pygame.QUIT: self.terminate = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_r: self.resetGame() if event.key == pygame.K_UP: if self.paddle2.y > 0: self.paddle2.setChange(-self.paddlespeed) if event.key == pygame.K_DOWN: if self.paddle2.y < constant.HEIGHT - self.paddle2.h: self.paddle2.setChange(self.paddlespeed) if event.key == pygame.K_w: if self.paddle1.y > 0: self.paddle1.setChange(-self.paddlespeed) if event.key == pygame.K_s: if self.paddle1.y < constant.HEIGHT - self.paddle1.h: self.paddle1.setChange(self.paddlespeed) if event.type == pygame.KEYUP: if event.key == pygame.K_UP or event.key == pygame.K_DOWN: self.paddle2.stop() elif event.key == pygame.K_w or event.key == pygame.K_s: self.paddle1.stop() print(event) def collisionCheck(self): if (self.ball.x >= self.paddle1.x + self.ball.r and self.ball.x <= self.paddle1.x + self.paddle1.w + self.ball.r ) and (self.ball.y >= self.paddle1.y and self.ball.y <= self.paddle1.y + self.paddle1.h): self.ball.a = random.uniform(math.pi * 7 / 4, math.pi * 9 / 4) self.ball.speed = random.randint(4, 8) #print(ball.a) self.ball.updateSpeeds() if (self.ball.x >= self.paddle2.x - self.ball.r and self.ball.x <= self.paddle2.x + self.paddle2.w - self.ball.r ) and (self.ball.y >= self.paddle2.y and self.ball.y <= self.paddle2.y + self.paddle2.h): self.ball.a = random.uniform(math.pi * 3 / 4, math.pi * 5 / 4) self.ball.speed = random.randint(4, 8) #print(ball.a) self.ball.updateSpeeds() def renderText(self): self.gameDisplay.fill((0, 0, 0)) self.gameDisplay.blit(self.text, self.textRect) self.gameDisplay.blit(self.text2, self.text2Rect) self.gameDisplay.blit(self.score1text, self.score1Rect) self.gameDisplay.blit(self.score2text, self.score2Rect) def updateText(self): self.score1text = self.font.render(str(self.player1.score), True, (255, 255, 255)) self.score2text = self.font.render(str(self.player2.score), True, (255, 255, 255)) self.score1Rect = self.score1text.get_rect() self.score1Rect.center = (40, 40) self.score2Rect = self.score2text.get_rect() self.score2Rect.center = (constant.WIDTH - 40, 40) def updateObject(self): self.paddle1.update() self.paddle2.update() self.ball.update() def checkScore(self): if self.ball.x <= self.ball.r: self.player2.winRound() self.ball.reset() self.paddle1.reset() elif self.ball.x >= constant.WIDTH - self.ball.r: self.player1.winRound() self.ball.reset() self.paddle2.reset() def renderObject(self): self.ball.render() self.paddle1.render() self.paddle2.render()