class DisplayGameCharacter: """Class that draws a character in the centre of a Pygame window.""" def __init__(self): """Initialize the game and create game resources.""" pygame.init() self.screen = pygame.display.set_mode((1200, 800)) pygame.display.set_caption("Exercise 12-2") self.game_character = GameCharacter(self) self.bg_color = (230, 230, 230) def run_game(self): """Start the main loop for the game.""" while True: self._check_events() self._update_screen() def _check_events(self): # Respond to keypresses and mouse events. for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() def _update_screen(self): # Update images on the screen and flip to the new image. self.screen.fill(self.bg_color) self.game_character.blitme() pygame.display.flip()
def __init__(self): """Initialize the game and create game resources.""" pygame.init() self.screen = pygame.display.set_mode((1200, 800)) pygame.display.set_caption("Exercise 12-2") self.game_character = GameCharacter(self) self.bg_color = (230, 230, 230)
def __init__(self): """Initialize the game, and create game resources.""" pygame.init() self.settings = Settings() self.screen = pygame.display.set_mode((1280, 720), pygame.SCALED) self.settings.screen_width = self.screen.get_rect().width self.settings.screen_height = self.screen.get_rect().height pygame.display.set_caption("Invasion Alien") # Create an instance to store game statistics. # and create a scoreboard. self.stats = GameStats(self) self.sb = Scoreboard(self) # the parameter "self" is an instance of InvasionAlien, # so it gives access to InvasionAlien instance info to Ship self.ship = Ship(self) self.bullets = pygame.sprite.Group() self.missiles = pygame.sprite.Group() self.character = GameCharacter(self) self.aliens = pygame.sprite.Group() self._create_fleet() # Set the background color. self.bg_color = (0, 0, 0) # Make the Play button. self.play_button = Button(self, "Jugar")
class BlueSky: def __init__(self): pygame.init() self.screen = pygame.display.set_mode((1200, 800)) pygame.display.set_caption("Blue Sky") self.bg_color = (0, 0, 230) self.game_character = GameCharacter(self) def run_game(self): while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() self.screen.fill(self.bg_color) self.game_character.blitme() pygame.display.flip()
def run_game(): """Init game and create a screen object.""" # 初始化所有导入的pygame对象 pygame.init() ai_settings = Settings() # 创建一个名为screen的窗口 (1200, 800)是一个元组, 指定游戏窗口尺寸 # screen 是一个 surface, 游戏中的每个元素(外星人飞船)都是一个surface # 激活游戏的动画循环后, 每经过一次循环都将自动重绘这个surface screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption('Alien Invasion') # 创建一个游戏logo game_character = GameCharacter(screen) # 创建Play按钮 play_button = Button(ai_settings, screen, "Play") # 创建一个用于存储游戏统计信息的实例 stats = GameStats(ai_settings) sb = ScoreBoard(ai_settings, screen, stats) # 创建一艘飞船, 一个子弹编组, 一个外星人编组 ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() # 创建外星人群 gf.create_fleet(ai_settings, screen, ship, aliens) # 开始游戏的主循环 while True: # 监视键盘和鼠标事件 # 事件是用户玩游戏时执行的操作, 如按键和移动鼠标 # 为让程序响应事件, 编写一个事件循环, 以侦听事件, 并根据发生的事件执行相应任务 gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: # 更新飞船位置, 未消失的子弹, 外星人位置 ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets) # 每次循环时都重绘屏幕 gf.update_screen(ai_settings, screen, stats, sb, ship, game_character, aliens, bullets, play_button)
def __init__(self): self.player = GameCharacter() self.article = None self.show_player() self.load_article(0)
"Memento example Use Case" from game_character import GameCharacter from caretaker import CareTaker GAME_CHARACTER = GameCharacter() CARETAKER = CareTaker(GAME_CHARACTER) # start the game GAME_CHARACTER.register_kill() GAME_CHARACTER.move_forward(1) GAME_CHARACTER.add_inventory("sword") GAME_CHARACTER.register_kill() GAME_CHARACTER.add_inventory("rifle") GAME_CHARACTER.move_forward(1) print(GAME_CHARACTER) # save progress CARETAKER.save() GAME_CHARACTER.register_kill() GAME_CHARACTER.move_forward(1) GAME_CHARACTER.progress_to_next_level() GAME_CHARACTER.register_kill() GAME_CHARACTER.add_inventory("motorbike") GAME_CHARACTER.move_forward(10) GAME_CHARACTER.register_kill() print(GAME_CHARACTER) # save progress CARETAKER.save()
def __init__(self): pygame.init() self.screen = pygame.display.set_mode((1200, 800)) pygame.display.set_caption("Blue Sky") self.bg_color = (0, 0, 230) self.game_character = GameCharacter(self)
flag = False return flag with open('input.txt') as fp: lines = fp.readlines() lines = [line.strip() for line in lines] N, M = map(int, lines[0].split(' ')) assert (3 <= N <= 50) and (3 <= M <= 50) pos_x, pos_y, direction = map(int, lines[1].split(' ')) assert (1 <= pos_x <= N) and (1 <= pos_y <= M) and (direction in [0, 1, 2, 3]) character = GameCharacter(pos_x, pos_y, direction) given_map = list() for line in lines[2:]: row = map(int, line.split(' ')) assert len(row) == M given_map.append(row) assert len(given_map) == N given_map = np.array(given_map) assert given_map[pos_x][pos_y] == 0 if DEBUG: print('INFO-DEBUG:: curr_pos: x={}, y={}'.format(pos_x, pos_y)) cnt = 0
from game_character import GameCharacter player = GameCharacter() def load_point(id=0): import yaml filename = "data/rooms/{}.yml".format(id) try: with open(filename) as room: point = yaml.load(room) except: point = {"description": "No description", } if id == 0: player.roll() return point def show_point(point): print(point) print("-"*80) print(point.get("description")) dialogue = point.get("dialogue") if dialogue: print() print(dialogue) for replica in dialogue: