def again(winner, lost, kicked): global repeat if kicked: text = font.render("You were kicked!", True, (10, 10, 10)) elif lost: text = font.render("You lost!", True, (10, 10, 10)) elif winner != '': text = font.render(winner, True, (10, 10, 10)) else: text = font.render("Game over", True, (10, 10, 10)) x = screen.get_size()[0] // 2 - text.get_size()[0] // 2 y = screen.get_size()[1] // 2 - text.get_size()[1] // 2 text_r = font.render('Press R to play again', True, (200, 200, 200)) x1 = screen.get_size()[0] // 2 - text_r.get_size()[0] // 2 y1 = y + text.get_size()[1] + 25 rep_loop = True while rep_loop: clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: rep_loop = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: rep_loop = False if event.key == pygame.K_r: rep_loop = False repeat = True screen.fill((255, 255, 255)) screen.blit(text, (x, y)) screen.blit(text_r, (x1, y1)) pygame.display.flip()
def menu(): global screen, clock, gamemode hello_text = 'Tanks 2D' hello_text = big_font.render(hello_text, True, (50, 50, 50)) buttons = [] single = Button('Single player', 100, 500, font, (0, 0, 0), (10, 200, 10), (6, 128, 6), start) buttons.append(single) multi = Button('Multiplayer', 330, 500, font, (0, 0, 0), (10, 200, 10), (6, 128, 6), start) buttons.append(multi) auto = Button('Autoplay', 550, 500, font, (0, 0, 0), (10, 200, 10), (6, 128, 6), start) buttons.append(auto) menuloop = True while menuloop: clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: menuloop = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: menuloop = False pos = pygame.mouse.get_pos() for button in buttons: dist_x = pos[0] - button.button_x dist_y = pos[1] - button.button_y if 0 <= dist_x <= button.button_w and 0 <= dist_y <= button.button_h: button.is_active = True if event.type == pygame.MOUSEBUTTONDOWN: gamemode = button.run(button.text) menuloop = False else: button.is_active = False screen.blit(poster, (-80, -80)) screen.blit( hello_text, (screen.get_size()[0] // 2 - hello_text.get_size()[0] // 2, 80)) for button in buttons: button.draw() pygame.display.flip()
def autoplay(): global clock, screen screen = pygame.display.set_mode((1000, 600)) rpc = RpcClient() rpc_response = {} for i in range(1, 31): rpc_response = rpc.room_register(f'room-{i}') if rpc_response.get('status', 200) == 200: break else: print(rpc_response) room, name = rpc_response['roomId'], rpc_response['tankId'] room_state = RoomEvents(room) room_state.start() ai = AI(name) counter = 0 seconds = 0 winner = '' lost = False kicked = False game_over = False mainloop = True while mainloop: millis = clock.tick(FPS) seconds += millis / 1000 for event in pygame.event.get(): if event.type == pygame.QUIT: mainloop = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: mainloop = False game_over = True if not room_state.ready: wait_text = 'Loading...' wait_text = big_font.render(wait_text, True, (50, 50, 50)) screen.fill((255, 255, 255)) text_rect = wait_text.get_rect(center=(screen.get_size()[0] // 2, screen.get_size()[1] // 2)) screen.blit(wait_text, text_rect) elif room_state.response: screen.fill((201, 175, 135)) cur_state = room_state.response tanks = cur_state['gameField']['tanks'] bullets = cur_state['gameField']['bullets'] ai.start(tanks, bullets) if ai.fire: rpc_response = rpc.fire() ai.fire = False ai.last_action = seconds if ai.turn_direction: print(ai.turn_direction) rpc_response = rpc.turn_tank(ai.turn_direction) ai.turn_direction = '' ai.last_action = seconds if ai.last_action + 10 < seconds: ai.turn_direction = random.choice(ai.directions) remaining_time = cur_state.get('remainingTime', 0) text = font.render(f'Remaining time: {remaining_time}', True, (0, 0, 0)) text_rect = text.get_rect(center=(400, 50)) screen.blit(text, text_rect) drawScoreboard(name, tanks, room) for tank in tanks: draw_tank(seconds, name, **tank) for bullet in bullets: draw_bullet(name, **bullet) if len(bullets) > counter: shoot_sound.play(maxtime=1600) counter = len(bullets) if room_state.new and cur_state['hits']: room_state.new = False explosion_sound.play() if next((x for x in cur_state['losers'] if x['tankId'] == name), None): lost = True mainloop = False game_over = True elif next((x for x in cur_state['kicked'] if x['tankId'] == name), None): kicked = True mainloop = False game_over = True elif cur_state['winners']: mainloop = False game_over = True win_text = 'Congrats! Score: {}. Winner(-s): '.format( cur_state["winners"][0]["score"]) winner = win_text + ', '.join( map( lambda i: i['tankId'] if i['tankId'] != name else 'You', cur_state['winners'])) elif next((x for x in room_state.winners if x['tankId'] == name), None): mainloop = False game_over = True win_text = 'Congrats! Score: {}. Winner(-s): '.format( room_state.winners[0]["score"]) winner = win_text + ', '.join( map( lambda i: i['tankId'] if i['tankId'] != name else 'You', room_state.winners)) elif not next((x for x in tanks if x['id'] == name), None): lost = True mainloop = False game_over = True pygame.display.flip() room_state.kill = True room_state.join() screen = pygame.display.set_mode((800, 640)) return game_over, winner, lost, kicked
def single(): global clock, screen bullets, tanks, walls = [], [], [] spawnpoints = [] free_spaces = [] with open('res/maps/map1.txt') as map: lines = map.readlines() i = 0 for line in lines: j = 0 for symb in line: if symb == '#': walls.append(Wall([j*32, i*32])) elif symb == '@': spawnpoints.append([j*32, i*32]) elif symb == '_': free_spaces.append([j*32, i*32]) j += 1 i += 1 tank1 = Tank(spawnpoints[0][0], spawnpoints[0][1], 800//6, (3, 102, 6), 32, 'Player 1', fire=pygame.K_RETURN) tank2 = Tank(spawnpoints[1][0], spawnpoints[1][1], 800//6, (135, 101, 26), 32, 'Player 2', d_right=pygame.K_d, d_left=pygame.K_a, d_up=pygame.K_w, d_down=pygame.K_s) # tank3 = Tank(100, 100, 800//6, (0, 0, 0xff), pygame.K_h, pygame.K_f, pygame.K_t, pygame.K_g, pygame.K_2) # tank4 = Tank(100, 200, 800//6, (0xff, 255, 0), pygame.K_l, pygame.K_j, pygame.K_i, pygame.K_k, pygame.K_3) tanks += [tank1, tank2] box = Box(0.05, free_spaces) cycle = 0 winner = '' game_over = False mainloop = True while mainloop: millis = clock.tick(FPS) seconds = millis / 1000 for event in pygame.event.get(): if event.type == pygame.QUIT: mainloop = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: mainloop = False game_over = True for tank in tanks: if event.key == tank.fire_key: bullets.append(Bullet(tank)) pressed = pygame.key.get_pressed() for tank in tanks: # print(tank.direction) # stay = True for key in tank.KEY.keys(): if pressed[key]: tank.changeDirection(tank.KEY[key]) tank.is_static = False # stay = False # if stay: # tank.is_static = True screen.fill((201, 175, 135)) # screen.blit(background, (0, 0)) for wall in walls: wall.draw() if box.is_active: box.draw() cycle += seconds if cycle >= box.interval: cycle = 0 box.cur_image += 1 box.cur_image %= len(box.images) elif box.wait < box.reload: box.wait += seconds else: box.newBox() box.wait = 0 box.is_active = True for i in range(len(tanks)): tanks[i].move(seconds, box, tanks) txt = small_font.render(f'{tanks[i].name}: {tanks[i].lifes} lifes, {tanks[i].score} points', True, (0, 0, 0)) screen.blit(txt, (5, i*txt.get_size()[1] + 5)) for j in range(len(walls)): if checkCollisions(tanks[i], walls[j], False, True): tanks[i].lifes -= 1 del walls[j] break for j in range(len(bullets)): if checkCollisions(tanks[i], bullets[j], False, False): explosion_sound.play() bullets[j].tank.score += 1 tanks[i].lifes -= 1 del bullets[j] break if tanks[i].lifes <= 0: del tanks[i] break for i in range(len(bullets)): bullets[i].move(seconds) if bullets[i].lifetime > bullets[i].destroytime: del bullets[i] break for j in range(len(walls)): if checkCollisions(bullets[i], walls[j], False, True): bullets[i].lifetime = 10 del walls[j] break pygame.display.flip() if len(tanks) == 0: winner = '' game_over = True mainloop = False if len(tanks) == 1: win_tank = tanks[0] winner = f'Congrats! Score: {win_tank.score}. Winner(-s): {win_tank.name}' game_over = True mainloop = False return game_over, winner, False, False