def main(): os.environ['SDL_VIDEO_WINDOW_POS'] = '400,200' SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480 SCREEN_BACKGROUND = 232, 232, 232 pygame.init() surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.DOUBLEBUF) pygame.display.set_caption('Pywander - save the world in less than 9 times') clock = pygame.time.Clock() board = StartupBoard() sound = SoundObject() sound.play('background', -1) while board is not None: surface.fill(SCREEN_BACKGROUND) events = pygame.event.get() for event in events: if event.type == pygame.locals.KEYDOWN and \ event.key == pygame.locals.K_ESCAPE: sys.exit(0) board = board.update(surface, events) fps = clock.get_fps() fps_label = LabelObject('FPS: %d' % fps, 14) fps_label.change_realign('bottom-left', left=15, bottom=25) fps_label.draw_on_surface(surface) pygame.display.flip() clock.tick(200)
def process_draw_on_surface(self, surface): self.background.draw_on_surface(surface) if self.labels == []: label = LabelObject('SORRY', 121) label.rect.left = 71 label.rect.top = 104 self.labels.append(label) label = LabelObject('BUT YOU LOSE. JUST DON''T CRY,', 27) label.rect.left = 71 label.rect.top = 224 self.labels.append(label) label = LabelObject('YOU CAN PLAY AGAIN! :)', 27) label.rect.left = 71 label.rect.top = 264 self.labels.append(label) for label in self.labels: label.draw_on_surface(surface)
def main(): os.environ['SDL_VIDEO_WINDOW_POS'] = '400,200' SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480 SCREEN_BACKGROUND = 232, 232, 232 pygame.init() surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.DOUBLEBUF) pygame.display.set_caption( 'Pywander - save the world in less than 9 times') clock = pygame.time.Clock() board = StartupBoard() sound = SoundObject() sound.play('background', -1) while board is not None: surface.fill(SCREEN_BACKGROUND) events = pygame.event.get() for event in events: if event.type == pygame.locals.KEYDOWN and \ event.key == pygame.locals.K_ESCAPE: sys.exit(0) board = board.update(surface, events) fps = clock.get_fps() fps_label = LabelObject('FPS: %d' % fps, 14) fps_label.change_realign('bottom-left', left=15, bottom=25) fps_label.draw_on_surface(surface) pygame.display.flip() clock.tick(200)
def process_draw_on_surface(self, surface): self.background.draw_on_surface(surface) if self.labels == []: label = LabelObject('CONGRATS', 81) label.rect.left = 61 label.rect.top = 114 self.labels.append(label) level_finished = self.level - 1 label = LabelObject('YOU HAVE JUST FINISHED LEVEL %d,' % level_finished, 27) label.rect.left = 61 label.rect.top = 204 self.labels.append(label) label = LabelObject('PREPARE FOR NEXT LEVEL!', 27) label.rect.left = 61 label.rect.top = 244 self.labels.append(label) for label in self.labels: label.draw_on_surface(surface)
def process_draw_on_surface(self, surface): self.background.draw_on_surface(surface) if self.labels == []: label = LabelObject('SORRY', 121) label.rect.left = 71 label.rect.top = 104 self.labels.append(label) label = LabelObject('BUT YOU LOSE. JUST DON' 'T CRY,', 27) label.rect.left = 71 label.rect.top = 224 self.labels.append(label) label = LabelObject('YOU CAN PLAY AGAIN! :)', 27) label.rect.left = 71 label.rect.top = 264 self.labels.append(label) for label in self.labels: label.draw_on_surface(surface)
def process_draw_on_surface(self, surface): self.background.draw_on_surface(surface) if self.labels == []: label = LabelObject('PYWANDER', 37) label.rect.left = 90 label.rect.top = 90 self.labels.append(label) label = LabelObject('IS A GAME...', 15) label.rect.left = 329 label.rect.top = 109 self.labels.append(label) label = LabelObject('WHERE YOU MUST SAVE THE WORLD IN', 15) label.rect.left = 60 label.rect.top = 170 self.labels.append(label) label = LabelObject('LESS THAN...', 25) label.rect.left = 410 label.rect.top = 170 self.labels.append(label) label = LabelObject('9 TIMES', 121) label.rect.left = 31 label.rect.top = 184 self.labels.append(label) for label in self.labels: label.draw_on_surface(surface) buffer = pygame.image.tostring(surface, 'RGB') self.buffer = buffer self.buffer_size = surface.get_size()
def process_draw_on_surface(self, surface): time_before = self.background_time if self.background_time != 0: self.background_time = pygame.time.get_ticks() elapsed = self.background_time - time_before else: self.background_time = pygame.time.get_ticks() elapsed = 0 self.background_x -= int(elapsed * self.background_speed) if self.background_x < -self.background_width: self.background_x += self.background_width for i in range(3): self.background.rect.left = int(self.background_x) + (self.background_width * (i - 1)) self.background.draw_on_surface(surface, self) score_label = LabelObject('Score: %d' % self.score, 14) score_label.change_realign('top-right', right=15, top=15) score_label.draw_on_surface(surface, self) life_label = LabelObject('Life: %d' % self.life, 14) life_label.change_realign('top-left', left=15, top=15) life_label.draw_on_surface(surface, self) life_label = LabelObject('Attempts left: %d' % self.attempts_left, 14) life_label.change_realign('top-center', top=15) life_label.draw_on_surface(surface, self) for bullet in self.bullets_group.sprites(): if bullet.image.rect.left >= 640: self.bullets_group.remove(bullet) else: bullet.draw_on_surface(surface, self) self.ship.draw_on_surface(surface, self) for enemy in self.enemy_group.sprites(): if isinstance(enemy, EnemySprite): if enemy.is_completed(): self.enemy_group.remove(enemy) enemy.draw_on_surface(surface, self) for inactive in self.inactive_group.sprites(): if isinstance(inactive, EnemySprite): if inactive.is_completed(): self.inactive_group.remove(inactive) continue inactive.draw_on_surface(surface, self) for hit in pygame.sprite.groupcollide(self.enemy_group, self.bullets_group, 0, 1): if isinstance(hit, AsteroidSprite): self.score += 9 if isinstance(hit, BulletSprite): hit.kill() if isinstance(hit, BossSprite): self.score += 13 hit.hit() self.boss_life = hit.life if hit.life <= 0: self.status = PLAYER_WON return False if isinstance(hit, EnemySprite): self.score += 7 hit.kill() hit.show_explosion() sound = SoundObject() sound.play('boom', 0) self.inactive_group.add(hit) hit = pygame.sprite.spritecollideany(self.ship, self.enemy_group) if hit: fatal = isinstance(hit, AsteroidSprite) or isinstance(hit, BossSprite) if not fatal: hit.kill() if isinstance(hit, EnemySprite): hit.show_explosion() sound = SoundObject() sound.play('boom', 0) self.life -= 25 else: self.status = PLAYER_LOSE else: self.read_level_info() if self.attempts_left == 0 or self.life == 0: self.status = PLAYER_LOSE buffer = pygame.image.tostring(surface, 'RGB') self.buffer = buffer self.buffer_size = surface.get_size()
def process_draw_on_surface(self, surface): time_before = self.background_time if self.background_time != 0: self.background_time = pygame.time.get_ticks() elapsed = self.background_time - time_before else: self.background_time = pygame.time.get_ticks() elapsed = 0 self.background_x -= int(elapsed * self.background_speed) if self.background_x < -self.background_width: self.background_x += self.background_width for i in range(3): self.background.rect.left = int( self.background_x) + (self.background_width * (i - 1)) self.background.draw_on_surface(surface, self) score_label = LabelObject('Score: %d' % self.score, 14) score_label.change_realign('top-right', right=15, top=15) score_label.draw_on_surface(surface, self) life_label = LabelObject('Life: %d' % self.life, 14) life_label.change_realign('top-left', left=15, top=15) life_label.draw_on_surface(surface, self) life_label = LabelObject('Attempts left: %d' % self.attempts_left, 14) life_label.change_realign('top-center', top=15) life_label.draw_on_surface(surface, self) for bullet in self.bullets_group.sprites(): if bullet.image.rect.left >= 640: self.bullets_group.remove(bullet) else: bullet.draw_on_surface(surface, self) self.ship.draw_on_surface(surface, self) for enemy in self.enemy_group.sprites(): if isinstance(enemy, EnemySprite): if enemy.is_completed(): self.enemy_group.remove(enemy) enemy.draw_on_surface(surface, self) for inactive in self.inactive_group.sprites(): if isinstance(inactive, EnemySprite): if inactive.is_completed(): self.inactive_group.remove(inactive) continue inactive.draw_on_surface(surface, self) for hit in pygame.sprite.groupcollide(self.enemy_group, self.bullets_group, 0, 1): if isinstance(hit, AsteroidSprite): self.score += 9 if isinstance(hit, BulletSprite): hit.kill() if isinstance(hit, BossSprite): self.score += 13 hit.hit() self.boss_life = hit.life if hit.life <= 0: self.status = PLAYER_WON return False if isinstance(hit, EnemySprite): self.score += 7 hit.kill() hit.show_explosion() sound = SoundObject() sound.play('boom', 0) self.inactive_group.add(hit) hit = pygame.sprite.spritecollideany(self.ship, self.enemy_group) if hit: fatal = isinstance(hit, AsteroidSprite) or isinstance( hit, BossSprite) if not fatal: hit.kill() if isinstance(hit, EnemySprite): hit.show_explosion() sound = SoundObject() sound.play('boom', 0) self.life -= 25 else: self.status = PLAYER_LOSE else: self.read_level_info() if self.attempts_left == 0 or self.life == 0: self.status = PLAYER_LOSE buffer = pygame.image.tostring(surface, 'RGB') self.buffer = buffer self.buffer_size = surface.get_size()
def process_draw_on_surface(self, surface): self.background.draw_on_surface(surface) if self.objects == []: label = LabelObject('HOW TO PLAY?', 30) label.rect.left = 190 label.rect.top = 25 self.objects.append(label) label = LabelObject('Move your ship by pressing up and down.', 18) label.rect.left = 120 label.rect.top = 120 self.objects.append(label) label = LabelObject('To shoot press spacebar.', 18) label.rect.left = 120 label.rect.top = 145 self.objects.append(label) image = ImageObject('ship.png') image.rect.left = 45 image.rect.top = 120 self.objects.append(image) image = ImageObject('arrows.png') image.rect.left = 49 image.rect.top = 176 self.objects.append(image) image = ImageObject('arrows.png') image.rect.left = 49 image.rect.top = 75 image.surface_to_draw = pygame.transform.flip( image.surface_to_draw, False, True) self.objects.append(image) label = LabelObject('But watch out for', 18) label.rect.left = 120 label.rect.top = 240 self.objects.append(label) label = LabelObject('THE BOSS!', 28) label.rect.left = 350 label.rect.top = 235 self.objects.append(label) label = LabelObject('You must kill him before he shows', 18) label.rect.left = 200 label.rect.top = 340 self.objects.append(label) label = LabelObject('9 TIMES', 68) label.rect.left = 240 label.rect.top = 370 self.objects.append(label) image = ImageObject('boss.png') image.rect.left = 39 image.rect.top = 300 self.objects.append(image) self.objects.append(image) for element in self.objects: element.draw_on_surface(surface) buffer = pygame.image.tostring(surface, 'RGB') self.buffer = buffer self.buffer_size = surface.get_size()