1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| import pgzrun import random
WIDTH = 500 HEIGHT = 500 pacman = Actor('向右的吃豆人') pacman.pos = [250, 250] bean1 = Actor('豆子') bean1.x = random.randint(100, 400) bean1.y = random.randint(100, 400) bean2 = Actor('豆子') bean2.x = random.randint(100, 400) bean2.y = random.randint(100, 400) bean3 = Actor('豆子') bean3.x = random.randint(100, 400) bean3.y = random.randint(100, 400) beans = [bean1, bean2, bean3] score = 0 def draw(): screen.blit('游戏背景', [0, 0]) pacman.draw() for i in beans: i.draw() screen.draw.text(str(score), [15, 15], color='orange', fontsize=30) def on_key_down(key): if key == keys.LEFT: pacman.image = '向左的吃豆人' if key == keys.RIGHT: pacman.image = '向右的吃豆人' if key == keys.UP: pacman.image = '向上的吃豆人' if key == keys.DOWN: pacman.image = '向下的吃豆人' if key == keys.SPACE: print(score) def update(): global score if pacman.image == '向左的吃豆人': pacman.x -= 5 if pacman.image == '向右的吃豆人': pacman.x += 5 if pacman.image == '向上的吃豆人': pacman.y -= 5 if pacman.image == '向下的吃豆人': pacman.y += 5 for i in beans: if pacman.colliderect(i): i.x = random.randint(100, 400) i.y = random.randint(100, 400) score += 1 print(score) if pacman.x < 0 or pacman.x > 500 or pacman.y < 0 or pacman.y > 500: score -= 3 print(score) pacman.pos = [250, 250] if score < 0: exit() pgzrun.go()
|