python 小鸡飞行小游戏
python 小鸡飞行小游戏
用空格键控制小鸡飞行
代码
import pygame.freetype import sys import random pygame.init() screen = pygame.display.set_mode((500, 400)) pygame.display.set_caption('chick game') png1 = pygame.image.load('chick.png') png1 = pygame.transform.scale(png1, (96, 88)) font1 = pygame.freetype.Font(None, 24) font2 = pygame.freetype.Font(None, 80) c1, c2 = 50, 50 w1, w2 = 300, 600 h1, h2 = 100, 150 sp = 1 sc = 0 p = 0 i = 0 while True: pygame.time.delay(20) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if p == 0 and event.key == 32: p = 1 elif p == 1 and event.key == 32: sp = -3 elif p == 2 and event.key == 32 and i == 30: p = 0 c1, c2 = 50, 50 w1, w2 = 300, 600 h1, h2 = 100, 150 sp = 1 sc = 0 i = 0 if p == 0: screen.fill((255, 255, 255)) screen.blit(png1, (c1, c2)) pygame.draw.rect(screen, (50, 200, 100), (w1, 0, 70, h1)) pygame.draw.rect(screen, (50, 200, 100), (w2, 0, 70, h2)) pygame.draw.rect(screen, (50, 200, 100), (w1, h1 + 150, 70, 400 - h1)) pygame.draw.rect(screen, (50, 200, 100), (w2, h2 + 150, 70, 400 - h2)) font1.render_to(screen, [30, 250], "click space to start") if p == 1: w1 -= 2 w2 -= 2 c2 += int(sp) sp += 0.2 if c2 > 340: p = 2 if 0 <= w1 <= 150 and not h1 <= c2 <= h1+50: p = 2 if 0 <= w2 <= 150 and not h2 <= c2 <= h2+50: p = 2 if w1 <= -70: w1 += 600 h1 = random.randint(50, 200) sc += 5 if w2 <= -70: w2 += 600 h2 = random.randint(50, 200) sc += 5 screen.fill((255, 255, 255)) screen.blit(png1, (c1, c2)) pygame.draw.rect(screen, (50, 200, 100), (w1, 0, 70, h1)) pygame.draw.rect(screen, (50, 200, 100), (w2, 0, 70, h2)) pygame.draw.rect(screen, (50, 200, 100), (w1, h1 + 150, 70, 400 - h1)) pygame.draw.rect(screen, (50, 200, 100), (w2, h2 + 150, 70, 400 - h2)) if p == 2: screen.fill((255, 255, 255)) font2.render_to(screen, [80, 50], "score:%d" % sc) font1.render_to(screen, [100, 250], "click space to play again") if i < 30: i += 1 pygame.display.update()
素材
chick.png