def create_status_box(self): frame = Frame(self.game.screen, Rect(660, 20, 200, 200)) data = { 'score': 0, 'level': 1, } self.status_box = DictBox(frame, data)
def test_dict_box(self): """Display data from a dictionary.""" dict_data = { 'Level':'20', 'Name':'Zeratul', 'HP':'400' } db = DictBox(self.frame, dict_data) db.draw() pygame.display.update()
def create_status_box(self): frame = Frame(self.screen, Rect(700, 20, 200, 50)) data = { 'lives': 3, 'level': self.current_level - 1, } self.status_box = DictBox(frame, data)
def create_status_box(self): frame = Frame(self.game.screen, Rect(660, 20, 200, 200)) return DictBox(frame, self.data)
def create_status_box(self): frame = Frame(self.screen, Rect(660, 20, 200, 200)) self.status_box = DictBox(frame, {'score':0})
class SnakeGame: def __init__(self, screen): self.screen = screen self.tile_factory = TileFactory('data/tiles.conf') self.level = None self.snake = None self.status_box = None self.events = None self.score = 0 self.create_level() self.create_snake() self.create_status_box() self.update_mode = self.update_ingame self.move_delay = MOVE_DELAY self.delay = MOVE_DELAY def create_snake(self): start_pos = Vector(5, 5) frame = Frame(self.screen, Rect(10, 10, 640, 512)) self.snake = SnakeSprite(frame, self.tile_factory, start_pos, self.level) self.snake.set_direction(RIGHT) def create_level(self): frame = Frame(self.screen, Rect(10, 10, 640, 512)) tmap = TiledMap(frame, self.tile_factory) self.level = SnakeLevel(LEVEL, tmap) self.level.place_random_fruit() def create_status_box(self): frame = Frame(self.screen, Rect(660, 20, 200, 200)) self.status_box = DictBox(frame, {'score':0}) def update_finish_moves(self): """finish movements before Game Over""" if not self.snake.is_moving(): pygame.display.update() time.sleep(1) self.events.exit_signalled() def update_ingame(self): self.delay -= 1 if self.delay <=0: self.delay = self.move_delay self.snake.move_forward() if self.snake.eaten and not self.snake.is_moving(): self.level.remove_fruit(self.snake.head.pos) self.level.place_random_fruit() self.status_box.data['score'] += 100 self.snake.eaten = None if self.snake.crashed: self.update_mode = self.update_finish_moves self.score = self.status_box.data['score'] def update(self): self.update_mode() self.snake.move() def draw(self): self.update() self.level.draw() self.snake.draw() self.status_box.draw() pygame.display.update() def run(self): self.events = EventGenerator() self.events.add_listener(FigureMoveListener(self.snake.set_direction)) self.events.add_listener(ExitListener(self.events.exit_signalled)) with draw_timer(self, self.events): self.events.event_loop()
def create_status_box(self): frame = Frame(self.screen, Rect(660, 20, 200, 50)) self.status_box = DictBox(frame, {'score': 0})
class SpaceRaceGame: def __init__(self, screen): self.screen = screen self.tile_factory = TileFactory() self.starscape = None self.level = None self.spaceship = None self.status_box = None self.events = None self.score = 0 self.create_starscape() self.create_level() self.create_status_box() self.update_mode = self.update_ingame self.move_delay = MOVE_DELAY self.delay = MOVE_DELAY def create_starscape(self): frame = Frame(self.screen, Rect(0, 0, 800, 600)) self.starscape = StarScape(frame) def create_level(self): frame = Frame(self.screen, Rect(10, 10, 640, 512)) self.level = SpaceshipLevel(frame, self.tile_factory) self.spaceship = SpaceshipSprite(frame, self.tile_factory, Vector(1, 1), self.level) def create_status_box(self): frame = Frame(self.screen, Rect(660, 20, 200, 50)) self.status_box = DictBox(frame, {'score': 0}) def update_finish_moves(self): """finish movements before Game Over""" if not self.spaceship.is_moving(): pygame.display.update() time.sleep(1) self.events.exit_signalled() def update_ingame(self): self.delay -= 1 if self.delay <= 0: self.delay = self.move_delay self.spaceship.move_forward() if self.spaceship.crashed: self.update_mode = self.update_finish_moves self.score = 0 if self.spaceship.finished: self.update_mode = self.update_finish_moves self.score = 1000 def draw(self): self.update_mode() self.screen.clear() self.starscape.draw() self.level.draw() self.spaceship.draw() self.status_box.draw() pygame.display.update() self.starscape.scroll() def run(self): self.events = EventGenerator() self.events.add_listener(FigureMoveListener(self.spaceship.set_direction)) self.events.add_listener(ExitListener(self.events.exit_signalled)) with draw_timer(self, self.events): self.events.event_loop()
class PacGame: def __init__(self): self.game = Game() self.level = None self.pac = None self.ghosts = [] self.status_box = None self.create_level() self.create_pac() self.create_ghosts() self.create_status_box() frame = Frame(self.game.screen, Rect(660, 220, 200, 200)) self.lives = BarDisplay(frame, self.game, 3, 'p') self.collided = False self.mode = None self.update_mode = self.update_ingame def create_level(self): tmap = TiledMap(self.game) level = create_maze(*RANDOM_LEVEL_SIZE) self.level = PacLevel(level, tmap) def create_pac(self): self.pac = Pac(self.game, PAC_START, self.level) self.pac.set_direction(RIGHT) def create_ghosts(self): self.ghosts = [] for pos in GHOST_POSITIONS: self.ghosts.append(Ghost(self.game, pos, self.level)) def reset_level(self): self.pac.sprite.pos = Vector(PAC_START) self.create_ghosts() def create_status_box(self): frame = Frame(self.game.screen, Rect(660, 20, 200, 200)) data = { 'score': 0, 'level': 1, } self.status_box = DictBox(frame, data) def check_collision(self): if self.pac.collision(self.ghosts): self.update_mode = self.update_die self.pac.die() self.collided = True def update_die(self): """finish movements""" if self.pac.sprite.finished: time.sleep(1) self.lives.decrease() if self.lives.value == 0: self.game.exit() else: self.reset_level() self.game.events.empty_event_queue() self.update_mode = self.update_ingame def update_level_complete(self): """finish movement""" if self.pac.sprite.finished: time.sleep(1) self.game.exit() def update_ingame(self): self.check_collision() if self.pac.eaten: self.status_box.data['score'] = self.pac.score self.pac.eaten = None self.score = self.pac.score if self.level.dots_left == 0: self.update_mode = self.update_level_complete def draw(self): self.update_mode() self.level.draw() self.pac.update() self.pac.draw() for g in self.ghosts: g.update() g.draw() self.status_box.draw() self.check_collision() def run(self): self.mode = self.update_ingame self.game.event_loop(figure_moves=self.pac.set_direction, draw_func=self.draw)
class SnakeGame: def __init__(self): self.game = Game() self.level = None self.snake = None self.status_box = None self.events = None self.score = 0 self.create_level() self.create_snake() self.create_status_box() self.update_mode = self.update_ingame self.move_delay = MOVE_DELAY self.delay = MOVE_DELAY def create_snake(self): start_pos = (5, 5) self.snake = SnakeSprite(self.game, start_pos, self.level) self.snake.set_direction(RIGHT) def create_level(self): tmap = TiledMap(self.game) self.level = SnakeLevel(LEVEL, tmap) self.level.place_random_fruit() def create_status_box(self): frame = Frame(self.game.screen, Rect(660, 20, 200, 200)) self.status_box = DictBox(frame, {'score': 0}) def update_finish_moves(self): """finish movements before Game Over""" if not self.snake.is_moving(): pygame.display.update() time.sleep(1) self.game.exit() def update_ingame(self): self.delay -= 1 if self.delay <= 0: self.delay = self.move_delay if not EASY: self.snake.move_forward() if self.snake.eaten and not self.snake.is_moving(): self.level.remove_fruit(self.snake.head.pos) self.level.place_random_fruit() self.status_box.data['score'] += 100 self.snake.eaten = None if self.snake.crashed: self.update_mode = self.update_finish_moves self.score = self.status_box.data['score'] def update(self): self.update_mode() self.snake.move() def draw(self): self.update() self.level.draw() self.snake.draw() self.status_box.draw() def run(self): self.game.event_loop(figure_moves=self.snake.set_direction, draw_func=self.draw)
def test_dict_box(self, frame): """Display data from a dictionary.""" dict_data = {'Level': '20', 'Name': 'Zeratul', 'HP': '400'} db = DictBox(frame, dict_data) db.draw() pygame.display.update()
class PacGame: def __init__(self, screen): self.screen = screen self.frame = Frame(self.screen, Rect(10, 10, 640, 512)) self.tile_factory = TileFactory('data/tiles.conf') self.level = None self.pac = None self.ghosts = [] self.status_box = None self.events = None self.create_level() self.create_pac() self.create_ghosts() self.create_status_box() frame = Frame(self.screen, Rect(660, 220, 200, 200)) self.lives = BarDisplay(frame, self.tile_factory, 3, 'p') self.collided = False self.mode = None self.update_mode = self.update_ingame def create_level(self): tmap = TiledMap(self.frame, self.tile_factory) self.level = PacLevel(LEVEL, tmap) def create_pac(self): start_pos = Vector(5, 5) self.pac = Pac(self.frame, self.tile_factory, PAC_START, self.level) self.pac.set_direction(RIGHT) def create_ghosts(self): self.ghosts = [] for pos in GHOST_POSITIONS: self.ghosts.append(Ghost(self.frame, self.tile_factory, pos, self.level)) def reset_level(self): self.pac.sprite.pos = PAC_START self.create_ghosts() def create_status_box(self): frame = Frame(self.screen, Rect(660, 20, 200, 200)) data = { 'score': 0, 'level':1, } self.status_box = DictBox(frame, data) def check_collision(self): if self.pac.collision(self.ghosts): self.update_mode = self.update_die self.pac.die() self.collided = True def update_die(self): """finish movements""" if self.pac.sprite.finished: time.sleep(1) self.lives.decrease() if self.lives.value == 0: self.events.exit_signalled() else: self.reset_level() self.events.empty_event_queue() self.update_mode = self.update_ingame def update_level_complete(self): """finish movement""" if self.pac.sprite.finished: time.sleep(1) self.events.exit_signalled() def update_ingame(self): self.check_collision() if self.pac.eaten: self.status_box.data['score'] = self.pac.score self.pac.eaten = None self.score = self.pac.score if self.level.dots_left == 0: self.update_mode = self.update_level_complete def draw(self): self.update_mode() self.level.draw() self.pac.update() self.pac.draw() for g in self.ghosts: g.update() g.draw() self.status_box.draw() pygame.display.update() self.check_collision() time.sleep(0.005) def run(self): self.mode = self.update_ingame self.events = EventGenerator() self.events.add_listener(FigureMoveListener(self.pac.move)) self.events.add_listener(ExitListener(self.events.exit_signalled)) with draw_timer(self, self.events): self.events.event_loop()