def __init__(self, screen_x, screen_y, width_height, hard_difficulty=False): # add difficulty flag later self.screen_x = screen_x self.screen_y = screen_y self.width_height = width_height # init snake self.reset_snek(hard_difficulty) # init food self.rat = Food(self.screen_x, self.screen_y, self.width_height, self.snek.getBody())
def __init__(self): self.items = [] for key, item in game.settings.get_setting(['toys']).items(): toy = Toy() toy.name = item['name'] toy.price = item['price'] toy.score = item['score'] self.items.append(toy) for key, item in game.settings.get_setting(['foods']).items(): food = Food() food.name = item['name'] food.price = item['price'] food.score = item['score'] self.items.append(food)
def hamiltonian_circuit(size, screen, apples, player): ''' A basic AI which creates a path which hits all places on the board and runs the snake game until the snake reaches the maximum length. ''' width, height = size length = 1 path = [[50, 50]] track = [] for rows in range(0, ((height - 50) // (2 * player.edge)) - 1): path += (build_path(width - 50, height - 50, rows, player)) final_y = height - 50 - player.edge while final_y > 50: path.append([50, final_y]) final_y -= player.edge index = 0 clock = pygame.time.Clock() apple = Food(size, apples) for apples in range(0, apple.apples): apple.apple_pos.append(apple.get_coordinates(player.track, [])) while True: x, y = pygame.mouse.get_pos() for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: if pygame.mouse.get_pressed( num_buttons=3)[0] and 0 <= x <= 50 and 0 <= y <= 50: return pygame.time.delay(25) clock.tick(50) screen.fill(colors.get('black')) track.insert(0, [path[index % len(path)][0], path[index % len(path)][1]]) if len(track) > length: track.pop() index += 1 if [path[index % len(path)][0], path[index % len(path)][1]] in apple.apple_pos: if length + 4 >= 1600: length = 1 track = [] else: length += 4 apple.apple_pos.pop( apple.apple_pos.index( [path[index % len(path)][0], path[index % len(path)][1]])) apple.apple_pos.append(apple.get_coordinates(track, [])) for apples in apple.apple_pos: pygame.draw.rect(screen, colors.get('dark_red'), [apples[0], apples[1], apple.edge, apple.edge]) pygame.draw.rect( screen, colors.get('red'), [apples[0] + 1, apples[1] + 1, apple.edge - 2, apple.edge - 2]) for cubes in track: pygame.draw.rect(screen, colors.get('dark_green'), [cubes[0], cubes[1], player.edge, player.edge]) pygame.draw.rect( screen, colors.get('green'), [cubes[0] + 1, cubes[1] + 1, player.edge - 2, player.edge - 2]) make_outline(size, screen) draw_arrow(screen) pygame.display.update()
def maingame(size, screen, base_font, player, banned_blocks, gamemode, length=1, direction=None, track=[]): ''' The main snake game ''' clock = pygame.time.Clock() apple = Food(size, (2 * gamemode[1]) + 1) dualist = Dualist(size, 1 if gamemode[2] else 0) poison_apple = P_Food(size, gamemode[0]) for apples in range(0, apple.apples): apple.apple_pos.append( apple.get_coordinates(player.track, banned_blocks)) for poison_apples in range(0, poison_apple.apples): poison_apple.apple_pos.append( poison_apple.get_coordinates(player.track, banned_blocks, apple.apple_pos)) while True: pygame.time.delay(75) clock.tick(15) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() keys = pygame.key.get_pressed() direction = player.direction_check(direction, keys) player.movement(direction) player.tracker(length) screen.fill(colors.get('black')) if [player.x_position, player.y_position] in apple.apple_pos: length += 4 dualist.length += 4 if length + dualist.length + len(banned_blocks) >= 1600: return length apple.apple_pos.pop( apple.apple_pos.index([player.x_position, player.y_position])) apple.apple_pos.append( apple.get_coordinates(player.track, banned_blocks)) if poison_apple.change_check(gamemode[0]): poison_apple.apple_pos.pop(0) poison_apple.apple_pos.append( poison_apple.get_coordinates(player.track, banned_blocks, apple.apple_pos)) draw_banned_blocks(banned_blocks, screen, player.edge) for cubes in player.track: pygame.draw.rect(screen, colors.get('dark_green'), [cubes[0], cubes[1], player.edge, player.edge]) pygame.draw.rect( screen, colors.get('green'), [cubes[0] + 1, cubes[1] + 1, player.edge - 2, player.edge - 2]) if gamemode[2]: dualist.movement(direction) dualist.tracker(dualist.length) for cubes in dualist.track: pygame.draw.rect( screen, colors.get('dark_orange'), [cubes[0], cubes[1], player.edge, player.edge]) pygame.draw.rect(screen, colors.get('orange'), [ cubes[0] + 1, cubes[1] + 1, player.edge - 2, player.edge - 2 ]) for apples in apple.apple_pos: pygame.draw.rect(screen, colors.get('dark_red'), [apples[0], apples[1], apple.edge, apple.edge]) pygame.draw.rect( screen, colors.get('red'), [apples[0] + 1, apples[1] + 1, apple.edge - 2, apple.edge - 2]) for poison_apples in poison_apple.apple_pos: pygame.draw.rect(screen, colors.get('dark_blue'), [ poison_apples[0], poison_apples[1], poison_apple.edge, poison_apple.edge ]) pygame.draw.rect(screen, colors.get('blue'), [ poison_apples[0] + 1, poison_apples[1] + 1, poison_apple.edge - 2, poison_apple.edge - 2 ]) make_outline(size, screen) if player.collision_check(banned_blocks, dualist.track, poison_apple.apple_pos): return length score = base_font.render(f'Length: {length}', True, colors.get('white')) screen.blit(score, (50, 10)) pygame.display.update()
the distance, but there is no way across the chasm."""), 'narrow': Room( "Narrow Passage", """The narrow passage bends here from west to north. The smell of gold permeates the air.""", False, True), 'treasure': Room( "Treasure Chamber", """You've found the long-lost treasure chamber! Sadly, it has already been completely emptied by earlier adventurers. The only exit is to the south.""", False), } # Declare all the items item_dict = { 'apple': Food('apple', 'a small to medium-sized conic apple', 'food', 1, 10), 'bread': Food('bread', 'a slice of bread', 'food', 1, 10), 'ring': Treasure('ring', 'a silver ring', 'treasure', 1, 50), 'lamp': LightSource('lamp', 'a lamp', 'light source', 1, 50), 'necklace': Treasure('necklace', 'a silver necklace', 'treasure', 1, 50), 'bracelet': Treasure('bracelet', 'a silver bracelet', 'treasure', 1, 50), 'largeTreasureChest': Treasure('bracelet', 'a large treasure chest', 'treasure', 1, 500), 'sword': Weapon('sword', 'a large ninja katana', 'weapon', 10, 10), 'knife':
('pick up', '- pick up "item name" - you will pick up the item and execute the \'items\' command'), ('drop', '- drop "item name" - you will drop or unequip the item and execute the \'items\' command'), ('unlock', '- unlock "exit name" - unlocks the exit if you have the respective key'), ('eat', '- eat "item name" - you will eat the food, restore health and execute the \'items\' command'), ('equip', '- equip "item name" - you will equip the item and will be able to cause more damage to monsters'), ('monsters', '- monsters - prints out a list of alive monsters in the room'), ('attack', '- attack "monster name" - you will attack the monster'), ('quit', '- quit - exits the game'), ] ) ROOMS = { 'dark_room': Room(name='Dark room', exits=[Exit(name='Wooden door', goes_to=None, is_locked=False)], monsters=None, items=[Food(name='Half-rotten apple', weight=1, health_points=1)]), 'long_hall': Room(name='Long hall', exits=[ Exit(name='Wooden door', goes_to=None, is_locked=False), Exit(name='Prison cell door', goes_to=None, is_locked=True), Exit(name='Rusty iron gate', goes_to=None, is_locked=True) ], monsters=[ Monster(name='Venomous spider', health=5, damage=1) ], items=[Key(name='Prison key', weight=1, unlocks_door=None)]), 'prison_cell': Room(name='Prison cell', exits=[Exit(name='Prison cell door', goes_to=None, is_locked=False)], monsters=[ Monster(name='Frenzied boar', health=6, damage=2), Monster(name='Ferocious wolf', health=4, damage=2),
class GameScene(Scene): def __init__(self, screen_x, screen_y, width_height, hard_difficulty=False): # add difficulty flag later self.screen_x = screen_x self.screen_y = screen_y self.width_height = width_height # init snake self.reset_snek(hard_difficulty) # init food self.rat = Food(self.screen_x, self.screen_y, self.width_height, self.snek.getBody()) # init walls (maybe) for harder difficulties def get_scene_type(self): return 2 def reset_snek(self, hard_difficulty): self.snake_x = (self.screen_x / 2) - (self.width_height / 2) self.snake_y = (self.screen_y / 2) - (self.width_height / 2) self.moveDirection = "up" self.snek = Snake(self.screen_x, self.screen_y, self.snake_x, self.snake_y, self.width_height, self.moveDirection, hard_difficulty) def check_snake_food_collision(self): snek_head_coords = self.snek.getHeadCoords() rat_coords = self.rat.getCoords() if snek_head_coords == rat_coords: self.snek.grow() self.rat.getNewCoords(self.snek.getBody()) return # None def has_snake_snake_collision(self): return self.snek.has_self_collision() def has_snake_wall_collision(self): snek_head_coords = self.snek.getHeadCoords() return snek_head_coords[0] < 0 or snek_head_coords[ 0] > self.screen_x or snek_head_coords[1] < 0 or snek_head_coords[ 1] > self.screen_y def drawBackground(): global screen_x, screen_y for i in range(0, screen_x, 20): pygame.draw.rect(gameDisplay, pygame.Color(255, 255, 255, 255), pygame.Rect(i, 0, 1, screen_y)) for j in range(0, screen_y, 20): pygame.draw.rect(gameDisplay, pygame.Color(255, 255, 255, 255), pygame.Rect(0, j, screen_x, 1)) def draw(self, surface): # background draw surface.fill((0, 0, 0)) for i in range(0, self.screen_x, 20): pygame.draw.rect(surface, pygame.Color(255, 255, 255, 255), pygame.Rect(i, 0, 1, self.screen_y)) for j in range(0, self.screen_y, 20): pygame.draw.rect(surface, pygame.Color(255, 255, 255, 255), pygame.Rect(0, j, self.screen_x, 1)) # food draw self.rat.draw(surface) # snake draw self.snek.draw(surface) def update(self, events): # scene handler should check whether exit/escape button has been pressed # snake handle events for event in events: if event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT: self.snek.set_move_direction("right") elif event.key == pygame.K_DOWN: self.snek.set_move_direction("down") elif event.key == pygame.K_LEFT: self.snek.set_move_direction("left") elif event.key == pygame.K_UP: self.snek.set_move_direction("up") elif event.key == pygame.K_ESCAPE: return 1 # opens pause menu # food update self.rat.update() # snake update self.snek.update() # collision update self.check_snake_food_collision() if self.has_snake_snake_collision(): return 2 # game over (easy) if self.has_snake_wall_collision(): return 3 # game over (hard) def handle_events(self, events): pass