用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - python代码库

吃豆人

2022-01-26 作者: 云代码会员举报

[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()

[源代码打包下载]




网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...