def un_stripInit(p, self): self.player2 = player.Player(p.x, p.y, p.tank_type, p.player_id, 'ONLINE', self.height, img=self.tank_image)
def run(self): """ Main game loop """ arena = Arena() _player = player.Player((self.width / 2, 200), player.PlayerStats()) _player.level = arena self.allGroup.add(_player.arm, _player) running = True while running: seconds = self.clock.tick( self.fps) / 1000.0 # seconds passed since last frame for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: _player.goLeft(seconds) if event.key == pygame.K_RIGHT: _player.goRight(seconds) if event.key == pygame.K_UP: _player.jump() if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT and _player.change_x < 0: _player.stop() if event.key == pygame.K_RIGHT and _player.change_x > 0: _player.stop() # If the _player gets near the right side, shift the world left (-x) if _player.rect.right >= 500: diff = _player.rect.right - 500 _player.rect.right = 500 arena.shift_world(-diff) # If the _player gets near the left side, shift the world right (+x) if _player.rect.left <= 120: diff = 120 - _player.rect.left _player.rect.left = 120 arena.shift_world(diff) self.allGroup.clear(self.screen, arena.background) self.allGroup.update(seconds) arena.update() arena.draw(self.screen) self.allGroup.draw(self.screen) pygame.display.flip() # update pygame display pygame.quit() # clean up
def get_scoreboard(self): """Fetches the top 10 players in the database, sorted by score.""" player_db = db_player.PlayerDB() player_db.connect() entries = player_db.get_players() player_db.close() player_list = [] for entry in entries: player = p.Player(entry["user_name"], 0, entry["user_score"], entry["user_level"]) player_list.append(player) player_list = sorted(player_list, key=lambda x: x.score)[::-1] return player_list[:9]
def test_friction_to_high(capsys) -> None: """ Test reaction to a too high friction constant """ PhysicsConsts.friction_const = 1 assert PhysicsConsts.friction_const == 1 # Layers layers = [ arena.FrictionLayer(np.ones((ArenaSettings.x, ArenaSettings.y)), PhysicsConsts.friction_const), arena.AirResistanceLayer(PhysicsConsts.drag_coefficient), ] # Arena arena = arena.Arena(ArenaSettings.x, ArenaSettings.y, layers=layers) # Players players = [player.Player(200, 200, ps.Player1Settings)] # Physic engine physics = Physics() physics.move_players(players)
def on_mouse_press(x, y, button, modifiers): if self.delay[0]: return if button != pyglet.window.mouse.LEFT: return for listener in self.click_listeners: if (listener[0][0] <= x <= listener[0][1]) and ( listener[1][0] <= y <= listener[1][1]): if listener[3] == "back": self.score_label.delete() self.start_menu() if listener[3] == "back2": for label in self.scoreboard_text: label.delete() self.start_menu() if listener[3] == "button": self.engine.generate_cards(self.card_count) player = p.Player("", len(self.engine.board.cards)) listener[2](self.engine.board, player) elif listener[3] == "submit": if len(self.player.name) == 0: self.name_label.text = "Please enter a name. (Keyboard)" return self.engine.submit_to_scoreboard( self.player.name, self.level, self.player.score) self.name_label.delete() self.score_label.delete() listener[2]() elif listener[3] == "card": current_card = listener[2] current_card.flip() self.flip_card(current_card) self.card_render() for sprite in self.sprites: sprite.draw() if self.is_picking: if current_card.content == self.flipped_cards[ 0].content: current_card.set_correct() self.flipped_cards[0].set_correct() self.flipped_cards = [] self.player.score += self.level * 2 self.score_label.text = str(self.player.score) if current_card.healing: self.player.health += 1 self.heal_sound.play() else: self.correct_sound.play() remaining_cards = len([ 1 for card in self.board.cards if not card.correct ]) if remaining_cards == 0: self.all_done_sound.play() self.card_delay = [True, 0, 120, "ending"] else: self.flipped_cards.append(current_card) self.player.health -= 1 if self.player.health == 0: self.flipped_cards = [] self.all_done_sound.play() self.delay = [True, 0, 120, "ending"] else: self.wrong_sound.play() self.delay = [True, 0, 45, "wrong"] else: self.flipped_cards.append(current_card) self.is_picking = not self.is_picking
black = (0, 0, 0) white = (255, 255, 255) grey = (120, 120, 120) red = (255, 0, 0) green = (0, 255, 0) blue = (0, 0, 255) pygame.init() gameDisplay = pygame.display.set_mode((displayWidth, displayHeight)) pygame.display.set_caption('Pypong') clock = pygame.time.Clock() playerObject = player.Player(displayWidth * 0.1, displayHeight * 0.45, './Sprites/playerRed.png', displayWidth, displayHeight) ballObject = ball.Ball(displayWidth, displayHeight) gameElements = [playerObject, ballObject] gameBlocks = [] def generateBlocks(): for i in range(0, blockRows): for j in range(0, blockColumns): blockX = 0.5 * displayWidth + 17 * j blockY = 0.1 * displayHeight + 17 * i gameBlocks.append(block.Block(blockX, blockY, 16, 16)) def updateObjects():
def handle_buttons(self): if self.buttons['p1_tank_type'].click: self.p1_tank_type = switch(self.p1_tank_type, self.tank_type_list) self.buttons['p1_tank_type'].txt_label.text = f'P1:{self.p1_tank_type}' elif self.buttons['p2_tank_type'].click: self.p2_tank_type = switch(self.p2_tank_type, self.tank_type_list) self.buttons['p2_tank_type'].txt_label.text = f'P2:{self.p2_tank_type}' elif self.buttons['set_ip'].click: self.ip = input('Give me new ip:') self.buttons['set_ip'].txt_label.text = f'IP:{self.ip}' elif self.buttons['gamemode'].click: self.game_mode = 'OFFLINE' if self.game_mode == 'ONLINE' else 'ONLINE' self.buttons['gamemode'].txt_label.text = self.game_mode self.buttons['set_ip'].active = not self.buttons['set_ip'].active self.buttons['set_ip'].visible = not self.buttons['set_ip'].visible if self.game_mode == 'ONLINE': self.buttons['p1_tank_type'].x = self.width/2 self.buttons['p2_tank_type'].active = False self.buttons['p2_tank_type'].visible = False else: self.buttons['p1_tank_type'].x = self.width/2-200 self.buttons['p2_tank_type'].active = True self.buttons['p2_tank_type'].visible = True elif self.buttons['play'].click: if self.game_mode == 'OFFLINE': self.player1 = player.Player(self.tank_image.anchor_x, self.height-self.tank_image.anchor_y, self.p1_tank_type, 0, self.game_mode, self.height, img=self.tank_image) self.player2 = player.Player(self.width-self.tank_image.anchor_x, self.tank_image.anchor_y, self.p2_tank_type, 1, self.game_mode, self.height, img=self.tank_image) self.current_screen = 1 else: if self.ip is not None: # Connecting with server self.n = network.Network(self.ip) self.this_client_id = int(self.n.getId()) print(self.this_client_id) if self.this_client_id == 0: self.player1 = player.Player(self.tank_image.anchor_x, self.height-self.tank_image.anchor_y, self.p1_tank_type, 0, self.game_mode, self.height, in_online_main_player=True, img=self.tank_image) else: self.player1 = player.Player(self.width-self.tank_image.anchor_x, self.tank_image.anchor_y, self.p1_tank_type, 1, self.game_mode, self.height, in_online_main_player=True, img=self.tank_image) self.n.send_player(self.player1) self.n.p2(self) self.current_screen = 1 threading.Thread(target=self.trade_info).start() else: self.display_message('Set your ip first!', 3) elif self.buttons['stats'].click: self.current_screen = 2 elif self.back_from_stats_button.click: self.current_screen = 0 for btn in self.buttons.values(): if btn.long_press: continue btn.click = False
def __init__(self) -> None: """Initialize the game with all the components.""" pg.init() pg.font.init() self.screen: graphics.Screen = graphics.Screen() # Statistics self.statistics = Statistics() # Sliders self.slides: List[graphics.Slider] = [ graphics.Slider(Slider("Drag coefficient", 50, 0, 100, 1150, 820)), graphics.Slider(Slider("Friction", 10000, 0, 30000, 1150, 880)), graphics.Slider(Slider('Player 1 mass', 1, 0.01, 2, 1300, 820)), graphics.Slider(Slider('Player 2 mass', 1, 0.01, 2, 1300, 880)), ] # Players self.players: List[player.Player] = [ player.Player(200, 200, ps.Player1Settings, mass=self.slides[2], phone=ConnectPhone()), player.Player(300, 300, ps.Player2Settings, mass=self.slides[3]), ] # Plots self.graphs: List[graphics.Graph] = [ graphics.Graph(self.players, "v"), graphics.Graph(self.players, "a", position=(1100, 400)) ] # Buttons self.buttons: List[graphics.Button] = [ graphics.Button( Button("Start!", 700, 600, 100, 50, Colors.GREEN.value, Colors.BLUE.value)) ] # End screen buttons self.end_btn: List[graphics.Button] = [ graphics.Button( Button("Retry", 600, 600, 100, 50, Colors.GREEN.value, Colors.BLUE.value)), graphics.Button( Button("Quit", 800, 600, 100, 50, Colors.RED.value, Colors.BLUE.value)), ] # Layers self.layers: List[arena.ArenaLayer] = [ arena.FrictionLayer(np.ones((ArenaSettings.x, ArenaSettings.y)), self.slides[1].get_value), arena.AirResistanceLayer(self.slides[0].get_value), ] # Arena self.arena: arena.Arena = arena.Arena(ArenaSettings.x, ArenaSettings.y, layers=self.layers) # Physic engine self.physics: Physics = Physics(self) # Input boxes self.input_boxes = [ graphics.InputBox(500, 400, 200, 32, text='player 1 name'), graphics.InputBox(800, 400, 200, 32, text='player 2 name') ] # Score self.score_table = [ graphics.Text( Texts("Win and draw probabilities", ScreenSettings.width / 2, 200, 115)), graphics.Text(Texts('', ScreenSettings.width / 2, 260, 80)), graphics.Text(Texts('', ScreenSettings.width / 2, 320, 80)), graphics.Text(Texts('', ScreenSettings.width / 2, 380, 80)), ]