python 金币小游戏
我最近用python的pygame做了一个金币小游戏
游戏规则:移动挡板接住金币
游戏截图:
代码如下:
import pygame.freetype import sys import random pygame.init() screen = pygame.display.set_mode((600, 400)) pygame.display.set_caption('game') p = 0 i1 = 0 s = 0 t = 0 f1 = pygame.freetype.Font('C:/windows/fonts/msyh.ttc', 100) f2 = pygame.freetype.Font(None, 30) img = pygame.image.load('img.png') img = pygame.transform.scale(img, (30, 30)) l = [] 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.MOUSEBUTTONDOWN: if p == 0: t = 0 p = 1 screen.fill((255, 255, 255)) if p == 0: f1.render_to(screen, [100, 130], '开始游戏') if p == 1: i1 += 1 if i1 >= 50//(int(t/30)+1): i1 = 0 if t < 200: t += 1 l.append([random.randint(50, 520), 50]) if t == 200 and not l: p = 2 for i in l: screen.blit(img, [i[0], i[1]]) i[1] += int(t/30)+1 x = pygame.mouse.get_pos()[0] if x < 40: x = 40 if x > 560: x = 560 pygame.draw.rect(screen, (60, 150, 250), (x-40, 320, 80, 20)) for i in l: if i[1] >= 300: if x-70 <= i[0] <= x+40: s += 1 l.remove(i) f2.render_to(screen, [30, 30], 'score:%d' % s) if p == 2: f1.render_to(screen, [80, 130], 'score:%d' % s) pygame.display.update()
游戏素材:
img.png: