猫史档案馆


【Python作品分享】金币游戏【作品秀】

用户:刘骏森刘骏森查看:0 回复:0 评论:0 创建时间:2022-08-14T11:28:39


【作品展示】

center_image

 

【作品介绍】

w,向上键 向上移动

s,向下键 向下移动

a,向左键 向左移动

d,向右键 向右移动

在规定时间内尽可能收集更多的金币,然后救出小猫即可胜利,否则失败。游戏结束后,按R键重玩,按Q键退出。

 

【作品源代码】

from pgzrun import *
import random,time

WIDTH,HEIGHT,game_state,score,time_left = 800,600,'start',0,30

cat = Actor('cat.png',(750,50))
duan = Actor('astronaut.png', (50, 550))
coin_list = []
for i in range(5):
    coin = Actor('coin',(random.randint(0,WIDTH),random.randint(100,HEIGHT)))
    coin_list.append(coin)

def draw():
    global game_state,score,time_left
    screen.clear()
    screen.blit('background',(0,0))
    if game_state == 'start':
        screen.draw.text('Space cat',color='white', center=(WIDTH / 2,HEIGHT / 2 - 50), fontsize=50)
        screen.draw.text('Press any key to start', color='white', center=(WIDTH / 2, HEIGHT / 2 + 50),fontsize=50)
    elif game_state == 'play':
        cat.draw()
        duan.draw()
        for coin in coin_list:
            coin.draw()
        screen.draw.text('Score:' + str(score),color='white',center=(75,25),fontsize=50)
        screen.draw.text('Time_left:' + str(time_left),color='white',center=(250,25),fontsize=50)
    elif game_state == 'gameover':
        if duan.colliderect(cat):
            if score:
                screen.draw.text('You score:' + str(score),color='white',center=(WIDTH / 2, HEIGHT / 2 - 50),fontsize=50)
            else:
                screen.draw.text('You didn\'t get any coin', color='white', center=(WIDTH / 2, HEIGHT / 2 - 50), fontsize=50)
        else:
            screen.draw.text('You lost the cat', color='white', center=(WIDTH / 2, HEIGHT / 2 - 50), fontsize=50)
        screen.draw.text('Press R to restart,press Q to exit',color='white',center=(WIDTH / 2,HEIGHT / 2 + 50),fontsize=50)
        if keyboard.r:
            game_state,score,time_left = 'start',0,30
            duan.pos = 50,550
        elif keyboard.q:
            exit()

def update():
    global score,coin_list
    if game_state == 'play':
        for coin in coin_list:
            if duan.colliderect(coin):
                score += 5
                coin.pos = random.randint(0, WIDTH), random.randint(100, HEIGHT)
        if (keyboard.w or keyboard.up) and duan.top > 50:
            duan.y -= 3
        if (keyboard.s or keyboard.down) and duan.y < HEIGHT:
            duan.y += 3
        if (keyboard.a or keyboard.left) and duan.left > 0:
            duan.x -= 3
        if (keyboard.d or keyboard.right) and duan.right < WIDTH:
            duan.x += 3

def update_time_left():
    global time_left,game_state
    if time_left:
        time_left -= 1
    else:
        game_state = 'gameover'
        clock.unschedule(update_time_left)

def on_key_down():
    global game_state
    if game_state == 'start':
        game_state = 'play'
        clock.schedule_interval(update_time_left,1)

go()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页