def run_game(): pygame.init() ai_settings = Settings() screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") play_button = Button(screen, 'Play') stats = GameStats(ai_settings) sb = Scoreboard(screen, ai_settings, stats) ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() pills = Pill(ai_settings, screen) gf.creat_fleet(ai_settings, screen, aliens) while True: gf.check_events(stats, play_button, aliens, bullets, ship, ai_settings, screen, sb) if stats.game_active: ship.update() gf.update_bullets(bullets, aliens, ai_settings, screen, ship, stats, sb) gf.update_aliens(ai_settings, aliens, ship, bullets, stats, screen, sb, pills) if pills.apperence: gf.update_pills(pills, ship, ai_settings, screen) gf.update_screen(ai_settings,stats,sb,screen,ship,bullets, aliens,play_button, pills)
def game(): pygame.init() play_intro() grid = loadtxt('maze.txt', dtype=str) rows, cols = grid.shape width, height = (24, 24) screen_size = (width*cols, height*rows) screen = pygame.display.set_mode(screen_size, 0, 32) block_list = [] for col in range(cols): for row in range(rows): value = grid[row][col] if value == 'X': pos = (col*width, row*height) block_list.append(Brick((width, height), pos)) elif value == 'S': pos = (col*width, row*height) block_list.append(Shield((width, height), pos)) elif value == '-' or value == '|' or value == 'N': pos = (col * width, row * height) block_list.append(Pill((width, height), pos)) elif value == 'P': pos = (col * width, row * height) block_list.append(Powerpill((width, height), pos)) elif value == 'Y': pos = (col * width, row * height) block_list.append(Pacman((width, height), pos)) elif value == 'b': pos = (col * width, row * height) block_list.append(Blinky((width, height), pos)) elif value == 'i': pos = (col * width, row * height) block_list.append(Inky((width, height), pos)) elif value == 'p': pos = (col * width, row * height) block_list.append(Pinky((width, height), pos)) elif value == 'c': pos = (col * width, row * height) block_list.append(Clyde((width, height), pos)) background = pygame.Surface(screen_size).convert() background.fill((0, 0, 0)) while True: for event in pygame.event.get(): if event.type == QUIT: exit() # pacman.move() # for tile in tiles: # pacman.collide(tile) screen.blit(background, (0, 0)) for blocks in block_list: blocks.draw(screen) # pacman.draw(screen) pygame.display.update()
def parseField(self, screenRows): for y in range(self.field.height()): for x in range(self.field.width()): if Pacman.isPacman(screenRows[y][x]): self.pacman = Pacman(self, (x, y), screenRows[y][x]) self.pacman.useAnimation(self.animation) self.field.add((x, y), self.pacman) if Wall.isWall(screenRows[y][x]): wall = Wall((x, y), screenRows[y][x]) self.walls.append(wall) self.field.add((x, y), wall) if Wall.isGate(screenRows[y][x]): self.gate = wall if Pill.isPill(screenRows[y][x]): pill = Pill((x, y), screenRows[y][x]) self.pills.append(pill) self.field.add((x, y), pill) if Ghost.isGhost(screenRows[y][x]): ghost = Ghost(self, (x, y), screenRows[y][x]) self.ghosts.append(ghost) self.field.add((x, y), ghost)
def build(self): r = self.brick.rect rshield = self.shield.rect rhportal = self.hportal.rect rvportal = self.vportal.rect rpowerpill = self.powerpill.rect w, h = r.width, r.height dx, dy = self.deltax, self.deltay for nrow in range(len(self.rows)): row = self.rows[nrow] for ncol in range(len(row)): col = row[ncol] if col == 'X': self.bricks.append(pygame.Rect(ncol * dx, nrow * dy, w, h)) elif col == 'o': self.shields.append( pygame.Rect(ncol * dx, nrow * dy, rshield.width, rshield.height)) elif col == 'h': self.hportals.append( pygame.Rect(ncol * dx, nrow * dy, rhportal.width, rhportal.height)) elif col == 'v': self.vportals.append( pygame.Rect(ncol * dx, nrow * dy, rvportal.width, rvportal.height)) elif col == 'p': x = ncol * dx + rpowerpill.width / 2 y = nrow * dy + rpowerpill.height / 2 big = False pill = Pill(self.screen, x, y, big) self.pills.add(pill) elif col == 'P': x = ncol * dx y = nrow * dy big = True pill = Pill(self.screen, x, y, big) self.pills.add(pill)
def reset(self): x, y = self.game.last_block if x == 0: self.scale_x = 5 self.scale_y = 1 self.position = 0, 0 self.active = False else: self.scale_x = 0.75 + random.random() * 2 self.scale_y = min(max(y - 50 + random.random() * 100, 50), 300) / 100.0 self.position = x + 80 + random.random() * 100, 0 self.active = True # random add pill if self.x > 1000 and random.random() > 0.6: self.floor.add(Pill(self)) self.game.last_block = self.x + self.width, self.height
def build(self, screen): dx, dy = self.deltax, self.deltay for number_of_rows in range(len(self.rows)): row = self.rows[number_of_rows] for number_of_columns in range(len(row)): location = row[number_of_columns] if location == 'W': brick = Brick(screen) brick.rect.x = number_of_columns * dx brick.rect.y = number_of_rows * dy self.bricks.add(brick) if location == 'R': pill = Pill(screen) pill.rect.x = number_of_columns * dx pill.rect.y = number_of_rows * dy self.pills.add(pill) if location == 'P': powerpill = PowerPill(screen) powerpill.rect.x = number_of_columns * dx powerpill.rect.y = number_of_rows * dy self.powerpills.add(powerpill)
def isPill(self, coordinates): return Pill.isPill(self.field.get(coordinates))