def setUp(self): pygame.init() screen = pygame.display.set_mode((800, 600)) self.gamevents = Gamevents(800, 600) self.score = Score(screen, 800, 600, 5) self.ball = Ball(380, 280, 800, 600) self.bat1 = Bat(5, 280, 800, 600) self.bat2 = Bat(775, 280, 800, 600)
def __init__(self, mode): Level.__init__(self) self.mode = mode self.score = [0, 0] self.score_font = load_font(None, 100) self.player1 = Paddle("Player1", 0, HEIGHT/2, PADDLE_WIDTH, PADDLE_HEIGHT, PLAYER1_CONTROLS) if mode == 2: self.player2 = Paddle("Player2", WIDTH-PADDLE_WIDTH, HEIGHT/2, PADDLE_WIDTH, PADDLE_HEIGHT, PLAYER2_CONTROLS) elif mode == 1: self.player2 = Paddle("Player2", WIDTH-PADDLE_WIDTH, HEIGHT/2, PADDLE_WIDTH, PADDLE_HEIGHT, None) self.ball = Ball("Ball", WIDTH/2, HEIGHT/2, 10, 10) self.add_object(self.player1, self.player2, self.ball)
class Game(GameObject): # On crée nos objets ball = Ball() player_paddle = Paddle(ball, True) enemy_paddle = Paddle(ball, False) # Indique si la boucle de jeu doit tourner ou non should_run = True # Lance les init de chaque objet au démarrage du jeu def init(self, screen: Surface): self.ball.init(screen) self.player_paddle.init(screen) self.enemy_paddle.init(screen) # Lance les update de chaque objet à chaque frame et gère la # relation entre les objets def update(self): self.ball.update() self.player_paddle.update() self.enemy_paddle.update() # gestion de la collision entre ball et les paddles compteur = 0 if self.ball.pos.x <= 0: if self.ball.as_rect().colliderect(self.player_paddle.as_rect()) == False: self.should_run = False
def load_ball(level_manager: LevelManager): layer = list(filter(lambda l: l["name"] == "player", level_manager.data["layers"]))[0] ball = list(filter(lambda obj: obj["name"] == "ball", layer["objects"]))[0] speed = get_value_from_properties("speed", 50, ball["properties"]) fallen = get_value_from_properties("fallen", False, ball["properties"]) moving = get_value_from_properties("moving", False, ball["properties"]) return Ball(ball["x"], ball["y"]-ball["height"], fallen, moving, speed)
class Game(GameObject): # On crée nos objets ball = Ball() player_paddle = Paddle(ball, True) enemy_paddle = Paddle(ball, False) # Indique si la boucle de jeu doit tourner ou non should_run = True # Lance les init de chaque objet au démarrage du jeu def init(self, screen: Surface): self.ball.init(screen) self.player_paddle.init(screen) self.enemy_paddle.init(screen) # Lance les update de chaque objet à chaque frame et gère la # relation entre les objets def update(self): self.ball.update() self.player_paddle.update() self.enemy_paddle.update()
class Game: ball = Ball() player_paddle = Paddle(ball, True) enemy_paddle = Paddle(ball, False, 794) should_run = True def init(self, screen: Surface): self.ball.init(screen) self.player_paddle.init(screen) self.enemy_paddle.init(screen) def update(self): self.ball.update() self.player_paddle.update() self.enemy_paddle.update() # Collision Ball/Pads if self.ball.get_collision_square().colliderect(self.player_paddle) or self.ball.get_collision_square().colliderect(self.enemy_paddle): self.ball.dir.x = -self.ball.dir.x self.ball.effect.play()
def _normal_game(self, screen, game_type, backgrd1): '''initializes and starts 2 player game or game against computer. Args: screen: pygame display mode settings game_type: String, selected game type: "pvp" or "computer" backgrd1: background image according to screen size ''' sprites = pygame.sprite.Group() background = pygame.image.load( os.path.join(dirname, "assets", backgrd1)) bat1 = Bat(5, self.scr_height / 2, self.scr_width, self.scr_height) bat2 = Bat(self.scr_width - 25, self.scr_height / 2, self.scr_width, self.scr_height) ball = Ball(320, 240, self.scr_width, self.scr_height) sprites.add(bat1) sprites.add(bat2) sprites.add(ball) game = Game(ball, bat1, bat2, self.scr_width, self.scr_height, self.sound) game.main(game_type, sprites, background, screen, 0, self.ai_lvl)
def _against_wall(self, screen, game_type, hiscore, backgrd2): '''initializes and starts against the wall game. Args: screen: pygame display mode settings game_type: String, selected game type: "wall" hiscore: Current hiscore in selected screen size. backgrd2: background image according to screen size ''' sprites = pygame.sprite.Group() background = pygame.image.load( os.path.join(dirname, "assets", backgrd2)) bat1 = Bat(5, self.scr_height / 2, self.scr_width, self.scr_height) ball = Ball(320, 240, self.scr_width, self.scr_height) sprites.add(bat1) sprites.add(ball) game = Game(ball, bat1, None, self.scr_width, self.scr_height, self.sound) hiscore = game.main(game_type, sprites, background, screen, hiscore, self.ai_lvl) return hiscore
class TestGamevents(unittest.TestCase): def setUp(self): pygame.init() screen = pygame.display.set_mode((800, 600)) self.gamevents = Gamevents(800, 600) self.score = Score(screen, 800, 600, 5) self.ball = Ball(380, 280, 800, 600) self.bat1 = Bat(5, 280, 800, 600) self.bat2 = Bat(775, 280, 800, 600) def test_tietokone_odotuksen_jalkeen_palauttaa_false(self): self.assertEqual(self.gamevents.wait(3), False) def test_tietokone_laukaisee_pallon(self): self.assertEqual(self.gamevents.launch(3, self.ball), True) def test_pallo_lahtee_liikkeelle_kun_peli_alkaa(self): self.assertEqual(self.gamevents.launch(0, self.ball), True) def test_against_wall_pallo_pysyy_poydalla_hiscore_ei_muutu(self): run, hiscore = self.gamevents.ball_on_table(self.ball, self.score, 4, 5, True) self.assertEqual(run, True) self.assertEqual(hiscore, 5) def test_pelaaja_saa_pisteen_tietokonetta_vastaan(self): self.ball.rect.x = 805 run, score = self.gamevents.is_p1_score(self.ball, 1, self.score, True, False, None) self.assertEqual(run, True) self.assertEqual(score, 2) def test_tietokone_voittaa_pelin(self): self.ball.rect.x = -44 run, score = self.gamevents.is_p2_score(self.ball, 4, self.score, True, 3, None) self.assertEqual(run, False) self.assertEqual(score, 5) def test_pallo_kimpoaa_mailasta_against_wall(self): self.ball.rect.x = 28 self.ball.set_velocity(-6, 1) self.bat1.set_y_position(275) self.ball.update() score = self.gamevents.collision_wall(self.ball, self.bat1, 1, None) self.assertGreater(self.ball.get_x_velocity(), 3) self.assertEqual(score, 2) def test_pallo_kimpoaa_taka_seinasta_against_wall(self): self.ball.rect.x = 0.75 * 800 - 50 self.ball.set_velocity(7, 1) self.ball.update() self.gamevents.collision_wall(self.ball, self.bat1, 2, None) self.assertLess(self.ball.get_x_velocity(), 0) def test_pallo_vaihtaa_suuntaa_pelaajan1_mailasta(self): self.ball.set_position(31, 120) self.ball.set_velocity(-8, 3) self.bat1.set_y_position(100) self.ball.update() self.gamevents.collision(self.bat1, self.bat2, self.ball, None, True) self.assertGreater(self.ball.get_x_velocity(), 6) def test_pallo_vaihtaa_suuntaa_pelaajan2_mailasta(self): self.ball.set_position(735, 120) self.ball.set_velocity(8, 3) self.bat2.set_y_position(100) self.ball.update() self.gamevents.collision(self.bat1, self.bat2, self.ball, None, False) self.assertLess(self.ball.get_x_velocity(), -6)
def createBalls(self, ballColors): self.balls = [] for i, color in enumerate(ballColors): position = (self.position[0] + 25, 50 * (4 - i) + 25) self.balls.append(Ball(color, position, 24))
def main(): # pygame.mixer.music.play() # midline = Rect(199,0,2,600,(0,0,0)) running = True ballsLeft = 2 state = constants.TITLE_SCREEN playButton = Rect(constants.gameW / 2 - 105, constants.gameH - 200, 210, 63, None, images.playButton) plunger = Rect(constants.gameW - 30, constants.gameH - 60, 20, 60, None, images.plunger) ball = Ball(380, 550, 10, constants.colors['ball']) # PREP FOR FLIPPERS leftX = -15 + constants.gameW / 2 - 45 rightX = 15 + constants.gameW / 2 + 45 + 2 * 35 flippers = [ Flipper(leftX, 550, 90, 20, 5 * math.pi / 36, -5 * math.pi / 36, constants.colors['flipper'], "L"), Flipper(rightX, 550, 90, 20, 31 * math.pi / 36, 41 * math.pi / 36, constants.colors['flipper'], "R") ] # PREP FOR BASES leftHigh, left2ndHigh = flippers[0].getHighestPoints() leftXRate = math.tan(flippers[0].angle) leftmostTop = [0, leftHigh[1] - leftHigh[0] * leftXRate] leftmostBot = [0, left2ndHigh[1] - left2ndHigh[0] * leftXRate] rightHigh, right2ndHigh = flippers[1].getHighestPoints() rightXRate = -math.tan(flippers[1].angle) rightmostTop = [ constants.gameW, rightHigh[1] - (constants.gameW - rightHigh[0]) * rightXRate ] rightmostBot = [ constants.gameW, right2ndHigh[1] - (constants.gameW - right2ndHigh[0]) * rightXRate ] bases = [ Polygon([leftmostTop, leftHigh, left2ndHigh, leftmostBot], flippers[0].angle, constants.colors['wall']), Polygon([rightmostTop, rightHigh, right2ndHigh, rightmostBot], flippers[1].angle, constants.colors['wall']) ] walls = [ Rect(0, 0, constants.gameW, 10, constants.colors['wall']), Rect(0, 0, 20, constants.gameH, constants.colors['wall']), Rect(constants.gameW - 40, 0, 40, constants.gameH, constants.colors['wall']) ] bumpers = [ Bumper(60, 60), Bumper(175, 145), Bumper(265, 130), Bumper(240, 210), Bumper(100, 270, 50, 50, None, images.burst, "superbumper") ] bricks = [Brick(30, 160, 95, 36, 30 + 48, 30 + 95 - 48)] while running: running = listen(running) if state == constants.TITLE_SCREEN: ctx.blit(images.menu, (0, 0)) playButton.go(ctx) if mouse.mouse['click'] and collisions.rectPoint( playButton, mouse.mouse['pos']): state = constants.STAGE_ONE elif state == constants.STAGE_ONE: if keyboard.controls['keySpace'] and ball.launching and ball.spd[ 1] == 0: ball.spd[1] = -14 if keyboard.controls['keyEnter']: ball.reset() ctx.fill(constants.colors['bg']) for b in bases: b.go(ctx) for w in walls: w.go(ctx) for f in flippers: f.go(ctx, ball) for bb in bumpers: bb.go(ctx) for bbb in bricks: bbb.go(ctx) gfxdraw.filled_polygon(ctx, [[350, 0], [400, 0], [400, 50]], constants.colors['releaser']) gfxdraw.aapolygon(ctx, [[350, 0], [400, 0], [400, 50]], constants.colors['releaser']) plunger.go(ctx) ctx.blit(images.button, (330, 320)) scoreTEXT = str(ballsLeft) + " | " + str(ball.score) scoreRender = constants.muli["30"].render( scoreTEXT, True, constants.colors['score']) scoreRECT = scoreRender.get_rect() scoreRECT.right = constants.gameW - 60 scoreRECT.top = 20 ctx.blit(scoreRender, scoreRECT) ball.go(ctx, flippers, bases, walls, bumpers, bricks) if ball.y > constants.gameH or ball.x < 0 or ball.x > constants.gameW: if ballsLeft > 0: ball.reset() ballsLeft -= 1 else: state = constants.GAME_OVER # Debug # pygame.draw.rect(ctx,constants.colors['white'],(leftX-35,550,1,1)) # pygame.draw.rect(ctx,constants.colors['white'],(rightX-35,550,1,1)) pygame.draw.rect(ctx, constants.colors['white'], (flippers[0].pivotX, flippers[0].y, 1, 1)) # midline.go(ctx) fpsTEXT = str(round(clock.get_fps(), 1)) fps = constants.muli["15"].render(fpsTEXT, True, constants.colors['black']) ctx.blit(fps, (25, 8)) elif state == constants.GAME_OVER: ctx.blit(images.gameOver, (0, 0)) playButton.go(ctx) scoreTEXT = str(ball.score) + " points." scoreRender = constants.muli["30"].render( scoreTEXT, True, constants.colors['score']) scoreRECT = scoreRender.get_rect() scoreRECT.center = (constants.gameW / 2, constants.gameH - 300) ctx.blit(scoreRender, scoreRECT) if mouse.mouse['click'] and collisions.rectPoint( playButton, mouse.mouse['pos']): state = constants.STAGE_ONE ball.score = 0 ballsLeft = 3 # Update Window pygame.display.update() # input() clock.tick(60) pygame.quit()
class TestBall(unittest.TestCase): def setUp(self): self.ball = Ball(380, 280, 800, 600) def test_konstruktori_asetti_pallon_haluttuun_kohtaan(self): self.assertEqual((self.ball.rect.x), 380) self.assertEqual((self.ball.rect.y), 280) def test_pallon_sijainnin_asetus(self): self.ball.set_position(100, 99) self.assertEqual((self.ball.rect.x), 100) self.assertEqual((self.ball.rect.y), 99) def test_pallon_nopeus_on_oikea_ja_se_muuttuu_oikein(self): self.ball.set_velocity(3, 2) self.assertEqual((self.ball.get_x_velocity()), 3) self.assertEqual((self.ball.get_y_velocity()), 2) self.ball.set_velocity(-2, -3) self.assertEqual((self.ball.get_x_velocity()), -2) self.assertEqual((self.ball.get_y_velocity()), -3) def test_pallon_nopeus_ei_nouse_liikaa_pos(self): self.ball.set_velocity(6, 9) self.ball.set_velocity(11, 11) self.assertEqual((self.ball.get_x_velocity()), 10) self.assertEqual((self.ball.get_y_velocity()), 9) def test_pallon_nopeus_ei_nouse_liikaa_neg(self): self.ball.set_velocity(-8, -8) self.ball.set_velocity(-11, -11) self.assertEqual((self.ball.get_x_velocity()), -10) self.assertEqual((self.ball.get_y_velocity()), -8) def test_pallon_tilanne_paivittyy(self): self.ball.set_position(50, 50) self.ball.set_velocity(4, 2) self.ball.update() self.assertEqual((self.ball.rect.x), 54) self.assertEqual((self.ball.rect.y), 52) def test_pallon_tilanne_paivittyy_ja_suunta_muuttuu_alas(self): self.ball.set_position(70, 1) self.ball.set_velocity(2, -3) self.ball.update() self.assertEqual((self.ball.rect.y), 0) self.ball.update() self.assertEqual((self.ball.rect.y), 3) def test_pallon_tilanne_paivittyyja_suunta_muuttuu_ylos(self): self.ball.set_position(85, 513) self.ball.set_velocity(2, 3) self.ball.update() self.assertEqual((self.ball.rect.y), 515) self.ball.update() self.assertEqual((self.ball.rect.y), 512)
def setUp(self): self.ball = Ball(580, 280, 800, 600) self.bat = Bat(775, 250, 800, 600) self.computer = ComputerAi(self.ball, self.bat, 800, "average")
class TestBall(unittest.TestCase): def setUp(self): self.ball = Ball(580, 280, 800, 600) self.bat = Bat(775, 250, 800, 600) self.computer = ComputerAi(self.ball, self.bat, 800, "average") def test_maila_ei_liiku_kun_pallo_menossa_pois(self): self.ball.set_velocity(-2, -3) self.ball.update() self.computer.ai_player_move(False) self.assertEqual((self.bat.rect.y), 250) def test_maila_liikkuu_ylos_kun_pallo_tulossa_kohti_ylos(self): self.ball.set_velocity(5, -3) self.ball.update() self.computer.ai_player_move(False) self.assertEqual((self.bat.rect.y), 246) def test_maila_liikkuu_alas_kun_pallo_tulossa_kohti_alas(self): self.bat.set_y_position(200) self.ball.set_velocity(5, 3) self.ball.update() self.computer.ai_player_move(False) self.assertEqual((self.bat.rect.y), 204) def test_ai_lyo_palloa_kun_x_nopeus_alle_7(self): self.ball.rect.x = 720 self.ball.set_velocity(4, 4) self.computer.ai_player_move(False) self.assertEqual(self.computer.ai_player_move(False), True) def test_ai_ei_lyo_palloa_kun_x_nopeus_yli_7(self): self.ball.rect.x = 720 self.ball.set_velocity(8, 4) self.computer.ai_player_move(False) self.assertEqual(self.computer.ai_player_move(False), False) def test_ai_ei_lyo_palloa_kun_maila_ei_ole_kohdalla(self): self.ball.rect.x = 720 self.ball.rect.y = 100 self.ball.set_velocity(4, 4) self.computer.ai_player_move(False) self.assertEqual(self.computer.ai_player_move(False), False) def test_maila_liikkuu_alas_kun_pallo_tulossa_kohti_easy(self): self.computer = ComputerAi(self.ball, self.bat, 800, "easy") self.bat.set_y_position(200) self.ball.set_velocity(4, 3) self.ball.update() self.computer.ai_player_move(False) self.assertEqual((self.bat.rect.y), 202) def test_maila_liikkuu_ylos_kun_pallo_tulossa_kohti_easy(self): self.computer = ComputerAi(self.ball, self.bat, 800, "easy") self.ball.set_velocity(6, -3) self.ball.update() self.computer.ai_player_move(False) self.assertEqual((self.bat.rect.y), 248)
def setUp(self): self.ball = Ball(380, 280, 800, 600)
from TGL import Window from objects.ball import Ball from objects.paddle import PlayerPaddle, EnemyPaddle from TGL.internal import Box import numpy if __name__ == "__main__": w = Window((160, 41)) ball = Ball(Box(79, 20, 2, 1), numpy.array([160, 41])) playerPaddle = PlayerPaddle(Box(0, 17, 1, 7), numpy.array([160, 41])) enemyPaddle = EnemyPaddle(Box(159, 17, 1, 7), numpy.array([160, 41])) w.addGameObject(ball) w.addGameObject(playerPaddle) w.addGameObject(enemyPaddle) w.rootGameObject = playerPaddle w.gameLoop()
class PongLevel(Level): """Level for the pong game.""" def __init__(self, mode): Level.__init__(self) self.mode = mode self.score = [0, 0] self.score_font = load_font(None, 100) self.player1 = Paddle("Player1", 0, HEIGHT/2, PADDLE_WIDTH, PADDLE_HEIGHT, PLAYER1_CONTROLS) if mode == 2: self.player2 = Paddle("Player2", WIDTH-PADDLE_WIDTH, HEIGHT/2, PADDLE_WIDTH, PADDLE_HEIGHT, PLAYER2_CONTROLS) elif mode == 1: self.player2 = Paddle("Player2", WIDTH-PADDLE_WIDTH, HEIGHT/2, PADDLE_WIDTH, PADDLE_HEIGHT, None) self.ball = Ball("Ball", WIDTH/2, HEIGHT/2, 10, 10) self.add_object(self.player1, self.player2, self.ball) def tick(self, game): self.logic(game) self.draw(game.screen) Level.tick(self, game) def logic(self, game): pressed = pygame.key.get_pressed() if pressed[pygame.K_ESCAPE]: game.change_level("MAIN MENU") if self.ball.ballrect.colliderect(self.player1.paddlerect): self.ball.bounce(x=True, speed_mult=1.5) self.player1.shrink(0.9) elif self.ball.ballrect.colliderect(self.player2.paddlerect): self.ball.bounce(x=True, speed_mult=1.5) self.player2.shrink(0.9) if self.ball.ballrect.left < 0: self.ball.respawn(1) self.player1.reset_size() self.player2.reset_size() self.score[1] += 1 elif self.ball.ballrect.right > WIDTH: self.ball.respawn(-1) self.player1.reset_size() self.player2.reset_size() self.score[0] += 1 if self.mode == 1: self.player2.auto(self.ball.ballrect) def draw(self, screen): screen.fill(color.BLACK) # Draw the scores self.draw_score(screen, 0) self.draw_score(screen, 1) # Draw the dividing line divider = pygame.Rect(WIDTH/2-10, 0, 20, HEIGHT) pygame.draw.rect(screen, color.WHITE, divider) def draw_score(self, screen, player): score = self.score_font.render(str(self.score[player]), True, color.WHITE) score_rect = score.get_rect(y=50) if player == 0: score_rect.x = WIDTH/2 - score_rect.width * 2 else: score_rect.x = WIDTH/2 + score_rect.width screen.blit(score, score_rect)