def run_game(): pygame.init() gameDisplay = pygame.display.set_mode((DISPLAY_W,DISPLAY_H)) pygame.display.set_caption('Learn to fly') running = True bgImg = pygame.image.load(BG_FILENAME) label_font = pygame.font.SysFont("monospace", DATA_FONT_SIZE) clock = pygame.time.Clock() dt = 0 game_time = 0 pi = Pipe(gameDisplay, DISPLAY_W, 300, PIPE_LOWER) while running: dt = clock.tick(FPS) game_time += dt gameDisplay.blit(bgImg, (0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: running = False update_data_labels(gameDisplay, dt, game_time, label_font) pi.update(dt) pygame.display.update()
pygame.display.set_caption("FlyBird") #生成一个像素鸟对象 play = Play(screen) #生成一组管道对象 pipe = Pipe() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == MOUSEMOTION: play.rect = event.pos screen.blit(py_setings.bg_image_0,(0,0)) screen.blit(pipe.pipe_1, pipe.rect_1) screen.blit(pipe.pipe_2, pipe.rect_2) screen.blit(py_setings.g_image, (0, 400)) screen.blit(play.image,play.rect) pipe.update() pygame.display.flip()
class App: def __init__(self): pyxel.init(width=WIDTH, height=HEIGHT, caption="Flappy Bird", fps=35) pyxel.load(assets) self.reset() pyxel.run(self.update, self.draw) def reset(self): # make game activate False until bird is operated self.activate_game = False self.bird = Bird() self.pipe = Pipe() self.score_count = 0 self.background = Background() def update(self): if self.activate_game: # RESTART GAME WITH R if pyxel.btnp(pyxel.KEY_R): self.reset() # Constantly check for hit, and update pipes self.hit() self.pipe.update() self.bird.update_bird() # QUIT GAME WITH Q OR ESC if pyxel.btnp(pyxel.KEY_Q): pyxel.quit() # before starting to flap if not self.activate_game: if pyxel.btnp(pyxel.KEY_SPACE) or pyxel.btnp(pyxel.KEY_UP): self.bird.update_bird() self.activate_game = True def display_score(self): # max of 4 pipes on the screen # count the score for each pipe you pass for i in range(4): if self.bird.location.x == self.pipe.pipes[i][0]: self.score_count += 1 # print the current score if not self.bird.hit and self.activate_game: pyxel.text(WIDTH // 2, HEIGHT // 3, str(self.score_count), 7) ''' Check for hits ''' def hit(self): for i in range(4): # 12.5 is the offset of the bird (by pixels) # pipes[i][0] indicates the closest pipe if self.pipe.pipes[i][ 0] + 12.5 > self.bird.location.x > self.pipe.pipes[i][ 0] - 12.5: if (self.bird.location.y > self.pipe.pipes[i][1] + BIRD_GAP or self.bird.location.y < self.pipe.pipes[i][1] + 140): # update hit, pipes and background self.bird.hit = True self.pipe.active = False self.background.active = False if self.bird.location.y > HEIGHT - 36: self.bird.hit = True self.pipe.active = False self.background.active = False ''' Handles the drawing ''' def draw(self): pyxel.cls(0) self.background.draw_background() # draw instructions for the game on the screen if not self.activate_game: pyxel.text(WIDTH / 2 - len((INS) * 2), HEIGHT // 3, INS, 7) self.pipe.draw_pipes() self.background.draw_floor() self.bird.draw_bird() self.display_score() if self.bird.hit is True: pyxel.text(WIDTH / 2 - (len(SCORE) * 2), HEIGHT // 3, SCORE + str(self.score_count), 7) pyxel.text(WIDTH / 2 - (len(RESTART) * 2), HEIGHT // 2, RESTART, 7)