def gameloop(self): self.setupBoard() playing = True while playing: self.clock.tick(self.fps) text1 = Text(str(self.move_count), size=int(35), color=black) text1.rect.center = (600, 450) text2 = Text(_("(h)elp"), size=50) text2.rect.topleft = (10, 10) self.text = Group((text1, text2)) if (len(self.escArea.prisoners.sprites()) == self.cell_count and self.guard.moving == False): pygame.time.wait(3000) playing = False while Gtk.events_pending(): Gtk.main_iteration() # Handle Input Events for event in pygame.event.get(): # this one is for the box in the top right marked X if event.type == pygame.QUIT: playing, self.running = False, False # and this one is for the "ESC" key if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: self.move_count = 0 playing = False self.makeMenu() self.cell_count = 1 if event.key == pygame.K_r: self.resetGame() elif event.key == pygame.K_h: self.help() # update sprites self.guards.update() self.escArea.prisoners.update() for i in range(0, self.cell_count): self.cells[i].prisoners.update() self.cells[i].getAdjHS().prisoners.update() self.cells[i].text.update() # draw everything self.screen.blit(self.background, (0, 0)) self.escArea.prisoners.draw(self.screen) for i in range(0, self.cell_count): a = self.cells[i] a.text.draw(self.screen) a.getAdjHS().text.draw(self.screen) a.prisoners.draw(self.screen) a.getAdjHS().prisoners.draw(self.screen) self.guards.draw(self.screen) self.text.draw(self.screen) # finally, refresh the screen pygame.display.flip()
def play(): global bombs_number global score global level global timer score = bombs_number - len(bombs) level_text = Text('Level:' + str(level), 30, BLACK, (0, 0)) score_text = Text('Score:' + str(score), 30, BLACK, (0, 0)) level_text.rect.x = score_text.rect.x = 10 level_text.rect.y = 20 score_text.rect.y = 50 land = pygame.Surface((SCREEN_LENGTH, LAND_WIDTH)) land.fill(GRAY) land.blit(level_text.image, level_text.rect) land.blit(score_text.image, score_text.rect) display.fill(LIGHT_BLUE) display.blit(land, (0, FLOOR)) display.blit(car.image, car.rect) bombs.draw(display) bombs.update(pygame.sprite.spritecollideany(car, bombs)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: exit() elif event.type == pygame.USEREVENT: timer += 1 for i in range(1, level + 1): bombs.add( Bomb(randint(0, SCREEN_LENGTH - BOMB_SIZE), -randint(BOMB_SIZE, BOMB_SIZE * level))) bombs_number += 1 if timer == 40: level += 1 timer = 0 keys = pygame.key.get_pressed() if keys[pygame.K_RIGHT] or keys[pygame.K_d]: car.rect.x += CAR_SPEED if keys[pygame.K_LEFT] or keys[pygame.K_a]: car.rect.x -= CAR_SPEED if car.rect.x > SCREEN_LENGTH - CAR_LENGTH: car.rect.x = SCREEN_LENGTH - CAR_LENGTH if car.rect.x < 0: car.rect.x = 0 clock.tick(FPS)
def makeMenu(self): self.new_game = False self.background = pygame.Surface(self.screen.get_size()).convert() self.background.fill(yellow) cell_text = Text(_("Cells"), size=int(160)) cell_text.rect.center = ((600, 450)) self.text = Group((cell_text)) prompt_text = Text(_("press any key to begin"), size=int(35)) prompt_text.rect.center = (600, 530) self.flashing_text = Group((prompt_text))
def menu(): record = max(results) results.clear() results.append(record) record_text = Text('Record:' + str(record), 80, RED, (SCREEN_LENGTH / 2, 300)) display.fill(WHITE) display.blit(play_button.image, play_button.rect) display.blit(record_text.image, record_text.rect) for event in pygame.event.get(): if event.type == pygame.QUIT: exit() elif event.type == pygame.MOUSEBUTTONDOWN: if play_button.rect.x <= event.pos[0] <= play_button.rect.x + BUTTON_SIZE[0] and \ play_button.rect.y <= event.pos[1] <= play_button.rect.y + BUTTON_SIZE[1]: play_button.button_down() elif event.type == pygame.MOUSEBUTTONUP: if play_button.down: play_button.button_up() while not pygame.sprite.spritecollideany(car, bombs): play() while True: game_over() clock.tick(FPS) pygame.display.update()
def mainloop(self): self.load_all() self.makeMenu() self.running = True count = 0 while self.running: self.clock.tick(self.fps) self.screen.blit(self.background, (0, 0)) while Gtk.events_pending(): Gtk.main_iteration() for event in pygame.event.get(): # this one is for the box in the top right marked X if event.type == pygame.QUIT: self.running = False # and this one is for the "ESC" key if event.type == pygame.KEYDOWN: if self.cell_count == 2 and not self.new_game: self.new_game = True if self.new_game == True: if self.cell_count == 9: self.new_game = False self.gameloop() self.cell_count += 1 if self.cell_count == 9: self.background.fill(black) text1 = Text(_("Congratulations"), color=white, size=120) text2 = Text(_("You finished in %s moves.") % str(self.move_count), color=white, size=60) text2.rect.top = text1.rect.bottom + 10 self.text = Group((text1, text2)) self.text.draw(self.background) if self.cell_count == 2: count += 1 if (count / (self.fps / 2)) % 2 == 1: self.flashing_text.draw(self.screen) pygame.display.flip()
def setPos(self, x, y): self.x = x self.y = y pygame.draw.circle(self.game.background, black, (x, y), 54) pygame.draw.circle(self.game.background, white, (x, y), 50) text = Text(str(self.species + 1), size=20) text.rect.center = (self.x, self.y - 20) self.text = Group((text))
def help(self): t = 40 a = Text(_("Try and get 1 of species in the yellow escape area."), size=t) b = Text(_("Click a cell to send the guard there."), size=t) b.rect.top = a.rect.bottom + 1 c = Text(_("Prisoners can escape iff the adjacent hiding space is"), size=t) c.rect.top = b.rect.bottom + 1 d = Text(' ' + _("red and empty or green and occupied."), size=t) d.rect.top = c.rect.bottom + 1 e = Text(_("Hit 'Esc' to return to the menu."), size=t) e.rect.top = d.rect.bottom + 1 f = Text(_("Press 'r' to reset the current game"), size=t) f.rect.top = e.rect.bottom + 1 text = Group((a, b, c, d, e, f)) helping = True while helping: self.screen.fill(white) text.draw(self.screen) while Gtk.events_pending(): Gtk.main_iteration() for event in pygame.event.get(): # this one is for the box in the top right marked X if event.type == pygame.QUIT: self.running = False # and this one is for the "ESC" key if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: helping = False pygame.display.flip()
def next_turn(self): self.process_deselect() announce = Text("Arial", 42, "NEXT TURN!", self.plan.map_surface) self.all_sprites.add(announce) self.draw() self.update() pygame.time.wait(1000) self.all_sprites.remove(announce) print("/**********************************************/") print("Next Turn!") self.next_char() for char in self.list_char: print(char) print(char.states) self.current_char.manage_states()
def setPos(self, x, y): self.x = x self.y = y self.prisoners.set_pos(self.x, self.y) # since we're at it, let's use our crayon to color on the board # this is so we can tell if a space is 'hostile' or 'friendly' color = red if self.HorF == "f": color = green pygame.draw.circle(self.game.background, black, (x, y), 54) pygame.draw.circle(self.game.background, color, (x, y), 50) text = Text(str(self.species + 1), size=20, color=white) text.rect.center = (self.x, self.y - 20) self.text = Group((text))
def death(self): for char in self.list_char: if char.hp[0] == 0: print('{} is dead.'.format(char.name)) self.all_sprites.remove(char.sprite) if char == self.current_char: self.next_turn() self.list_char.remove(char) if len(self.list_char) < 2: print('END OF THE GAME!') announce = Text("Arial", 42, "END OF THE GAME!", self.plan.map_surface) self.all_sprites.add(announce) self.draw() self.update() pygame.time.wait(3000) self.running = False
def game_over(): global level global bombs_number global timer timer = 0 level = 1 bombs_number = 0 results.append(score) score_text = Text('Score:' + str(score), 80, RED, (SCREEN_LENGTH / 2, 150)) display.fill(WHITE) display.blit(menu_button.image, menu_button.rect) display.blit(score_text.image, score_text.rect) display.blit(game_over_text.image, game_over_text.rect) for bomb in bombs: bomb.kill() for event in pygame.event.get(): if event.type == pygame.QUIT: exit() elif event.type == pygame.MOUSEBUTTONDOWN: if menu_button.rect.x <= event.pos[0] <= menu_button.rect.x + BUTTON_SIZE[0] and \ menu_button.rect.y <= event.pos[1] <= menu_button.rect.y + BUTTON_SIZE[1]: menu_button.button_down() elif event.type == pygame.MOUSEBUTTONUP: if menu_button.down: menu_button.button_up() while True: menu() pygame.display.update() clock.tick(FPS)
clock = pygame.time.Clock() FPS = 60 car = Car() bombs = pygame.sprite.Group() bombs_number = 0 score = bombs_number - len(bombs) results = [0] level = 1 timer = 0 play_text = Text('PLAY', 80, RED, (BUTTON_SIZE[0] / 2, BUTTON_SIZE[1] / 2)) play_button = Button(GREEN, play_text, BUTTON_SIZE, (SCREEN_LENGTH / 2, 150)) menu_text = Text('MENU', 80, RED, (BUTTON_SIZE[0] / 2, BUTTON_SIZE[1] / 2)) menu_button = Button(GREEN, menu_text, BUTTON_SIZE, (SCREEN_LENGTH / 2, 300)) game_over_text = Text('GAME OVER', 100, RED, (SCREEN_LENGTH / 2, 50)) def menu(): record = max(results) results.clear() results.append(record) record_text = Text('Record:' + str(record), 80, RED, (SCREEN_LENGTH / 2, 300))
def main(): global polygon, polygon_line, coord_textfield, title_textfield, instructions_textfield, all, state, offset_y screen, background = init_window() offset_y = 0.2*screen.get_height() separation_line_width = 4 last_state = state # The text rendered on the screen coord_textfield = Text("x = 0 y = 0", font_size = 14) coord_textfield.set_position(5, offset_y) title_textfield = Text("Draw the polygon", font_size = 30) title_textfield.set_anchor((screen.get_width()/2, 0.25*offset_y)) instructions_textfield = Text("Click to begin drawing the polygon, press escape/backspace to cancel", font_size = 20) instructions_textfield.set_anchor((screen.get_width()/2, 0.6*offset_y)) # The newly added line to the polygon and the group with the other lines separating_line = Line((0, offset_y - separation_line_width), (screen.get_width(), offset_y - separation_line_width), pygame.color.Color('black'), separation_line_width) # separating_line = Line((15, 15), (15, 155), (0, 0, 0)) polygon_line = None polygon = pygame.sprite.OrderedUpdates(); # A group with all sprites for a more efficient rendering all = pygame.sprite.RenderUpdates(separating_line, title_textfield, instructions_textfield, coord_textfield) all.update() all.draw(screen) pygame.display.update() done = False clock = pygame.time.Clock() # Loop to run the game into while not done: # Fps lock clock.tick(60) # Flags to catch the input clicked = False canceled = False skip = False restart = False # Now check for events for event in pygame.event.get(): if event.type == pygame.QUIT: done = True elif event.type == pygame.MOUSEBUTTONDOWN: clicked = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE or event.key == pygame.K_BACKSPACE: canceled = True elif event.key == pygame.K_SPACE: skip = True elif event.key == pygame.K_r and state == "finished": restart = True # Act accordingly to the current step if state == "draw polygon": draw_polygon(clicked, canceled) elif state == "pick point": pick_point(clicked, canceled) elif state == "animate": animate_polygon(skip) elif state == "finished": if restart: state = "draw polygon" if(state != last_state): on_state_changed() last_state = state # Clear the screen where necessary all.clear(screen, background) # Update the sprites all.update() # Draw onto the screen dirty = all.draw(screen) pygame.display.update(dirty) pygame.quit()