[python]代码库
import pgzrun
import random
from pgzero import music
from pgzero.actor import Actor
from pgzero.clock import clock
from pgzero.keyboard import keyboard
TITLE = '疯狂吃豆人'
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
HP = 3
level = 0
state = 1
Pscore = []
def draw():
screen.clear()
screen.blit('游戏背景',(0, 0))
pacman.draw()
for i in beans:
i.draw()
screen.draw.text(str(score), [15,15], color='white', fontsize=35)
screen.draw.text(str(HP), [15,45], color='red', fontsize=35)
screen.draw.text(str(int(level+1)), [15,75], color='yellow', fontsize=35)
state1 = 0
def on_key_down(key):
global score,HP,level,state,state1,Pscore
if state == 1:
if key == keys.LEFT:
pacman.image = '向左的吃豆人'
state1 = 1
if key == keys.RIGHT:
pacman.image = '向右的吃豆人'
state1 = 1
if key == keys.UP:
pacman.image = '向上的吃豆人'
state1 = 1
if key == keys.DOWN:
pacman.image = '向下的吃豆人'
state1 = 1
if state == 0 and key == keys.SPACE:
HP = 3
level = 0
score = 0
state1 = 0
state = 1
def update():
global score,HP,level,state,state1,Pscore
if state == 1:
if state1 == 1:
if pacman.image == '向左的吃豆人':
if 5+level <=10:
pacman.x -= 5+level
else:
pacman.x -= 10
if pacman.image == '向右的吃豆人':
if 5+level <=10:
pacman.x += 5+level
else:
pacman.x += 10
if pacman.image == '向上的吃豆人':
if 5+level <=10:
pacman.y -= 5+level
else:
pacman.y -= 10
if pacman.image == '向下的吃豆人':
if 5+level <=10:
pacman.y += 5+level
else:
pacman.y += 10
for i in beans:
if pacman.colliderect(i):
i.x = random.randint(100, 400)
i.y = random.randint(100, 400)
score += 1
if score%50 == 0:
level = score/50
HP += 1
if pacman.x < 0 or pacman.x > 500 or pacman.y < 0 or pacman.y > 500:
HP -= 1
state1 = 0
pacman.pos = 250, 250
if HP <= 0:
state = 0
Pscore.append(score)
print('您的最终得分为',score,'等级为',int(level+1))
print(Pscore)
pgzrun.go()
[源代码打包下载]