猫史档案馆


【Python作品分享】寻猫历险记

用户:W莫斯科客MW莫斯科客M查看:0 回复:0 评论:0 创建时间:2022-08-13T23:45:23


【作品展示】

center_image

 

【作品介绍】

寻猫历险记:

寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻猫!寻

 

【作品源代码】

import pgzrun
from random import randint

WIDTH = 750
HEIGHT = 600
get_cat = False
score = 5

time_left = 45
game_over = False

aduan = Actor("astronaut")
aduan.pos = 100, 500

cat = Actor("cat")
cat.pos = randint(100, 700), randint(400, 600)

game_start = False

def draw():
    global game_start
    if game_start:
        screen.blit('background', (0, 0))
        if score == 5:
            screen.draw.text("Number of clicks left:" + str(score), color="#33ff00",
                         center=(WIDTH / 4, 80), fontsize=50)
        elif score == 4:
            screen.draw.text("Number of clicks left:" + str(score), color="#ffcc00",
                         center=(WIDTH / 4, 80), fontsize=50)
        elif score == 3:
            screen.draw.text("Number of clicks left:" + str(score), color="orange",
                         center=(WIDTH / 4, 80), fontsize=50)
        elif score == 2:
            screen.draw.text("Number of clicks left:" + str(score), color="#ff6600",
                         center=(WIDTH / 4, 80), fontsize=50)
        elif score == 1:
            screen.draw.text("Number of clicks left:" + str(score), color="#cc0000",
                         center=(WIDTH / 4, 80), fontsize=50)
        else:
            screen.draw.text("Number of clicks left:" + str(score), color="#999999",
                         center=(WIDTH / 4, 80), fontsize=50)

        aduan.draw()

        if game_over:
            music.stop()
            screen.draw.text("GAME OVER!", color="red",
                         center=(WIDTH / 2, HEIGHT / 2), fontsize=150)
            screen.draw.text("You can't get the cat!", color="#ff0000",
                         center=(WIDTH / 2, HEIGHT / 2 + 60), fontsize=150 / 2)

        elif time_left > 36:
            screen.draw.text("Time Left :" + str(time_left),
                         color="#33cc00", topleft=(10, 10),
                         fontsize=50)
        elif time_left < 27 and time_left > 18:
            screen.draw.text("Time Left :" + str(time_left),
                         color="orange", topleft=(10, 10),
                         fontsize=50)
        elif time_left > 18:
            screen.draw.text("Time Left :" + str(time_left),
                         color="#ffcc00", topleft=(10, 10),
                         fontsize=50)
        elif time_left > 9:
            screen.draw.text("Time Left :" + str(time_left),
                         color="#ff6600", topleft=(10, 10),
                         fontsize=50)
        else:
            screen.draw.text("Time Left :" + str(time_left),
                         color="#ff6600", topleft=(10, 10),
                         fontsize=50)
        if get_cat:
            cat.draw()
            screen.draw.text("You are win!", color="yellow",
                         center=(WIDTH / 2, HEIGHT / 2), fontsize=150)
            screen.draw.text("You are win!", color="orange",
                             center=(WIDTH / 2, HEIGHT / 2), fontsize=160)
    else:
        screen.draw.text("Get The Cat!", color="orange",
                         center=(WIDTH / 2, HEIGHT / 2 - 60), fontsize=160)
        screen.draw.text("Get The Cat!", color="#ffff00",
                         center=(WIDTH / 2, HEIGHT / 2 - 60), fontsize=150)
        screen.draw.text("Press R to start !", color="#cccccc",
                         center=(WIDTH / 2, HEIGHT / 2 + 60), fontsize=70)
        if keyboard.R:
            game_start = True
            music.play('dududu')
            clock.schedule_interval(update_time_left, 1.0)


def update():
    global get_cat, score, game_over, time_left
    if get_cat == False and game_over == False:
        if keyboard.A:
            aduan.x -= 2
        elif keyboard.d:
            aduan.x += 2
        if keyboard.W:
            aduan.y -= 2
        elif keyboard.S:
            aduan.y += 2
        distance = aduan.distance_to(cat)
        if distance <= 400:
            music.set_volume(1 - distance / 440)
        else:
            music.set_volume(0.1)
        if distance < 90 and keyboard.u and score > 0:
            get_cat = True
            music.pause()
            music.play_once('success')

        if score < 0:
            game_over = True


def on_key_down(key):
    global score, game_over
    if not get_cat:
        if key == key.U:
            if score > 0:
                score -= 1
            else:
                game_over = True


def update_time_left():
    global time_left, game_over, get_cat
    if get_cat == False and game_over == False:
        if time_left > 0:
            time_left -= 1
        else:
            game_over = True

pgzrun.go()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页