Python做的接苹果小游戏

nr0728 2021-10-28 17:04:26 2021-10-28 17:04:42 5

代码如下(包括URSINA库):

    import ursina.color
from ursina import *

import random

import time

app = Ursina()
window.fullscreen = 1.0


def input(key):
    if (key == "escape" or key == "left mouse down"):
        quit()
    if (key == "right mouse down"):
        Audio(sound_file_name="ImageBox/background.mp3")

def update():
    global apple_dy, basket_dx, score, apple_color, nums, startc
    nowc = time.time() - startc
    apple.y += time.dt * apple_dy
    basket.x += held_keys["right arrow"] * time.dt * basket_dx
    basket.x -= held_keys["left arrow"] * time.dt * basket_dx
    hit_info = apple.intersects()
    if hit_info.hit:
        Audio(sound_file_name="ImageBox/appleplus.wav")
        apple.x = random.randint(-4, 4)
        apple.y = 4
        if apple_color == "ImageBox/GreenApple.png":
            score += 2
            nums += 1
        elif apple_color == "ImageBox/RedApple.png":
            score += 1
            nums += 1
        if not score == 1:
            print_on_screen(f"You had {nums} Apples and {score} scores.", scale=1, duration=3, position=(-.8, .45))
        else:
            print_on_screen(f"You had 1 Apple and {score} score(s).", scale=1, duration=3, position=(-.8, .45))
    if apple.y < -2.6:
        score = 0
        nums = 0
        Audio(sound_file_name='ImageBox/game over.wav')
        print_on_screen(f"Game Over! You lost a apple, game is restart.", position=(0, 0), scale=2, origin=(0, 0))
        print_on_screen(f"You had {score} Apples.", scale=1, duration=3, position=(-.8, .45))
        apple.x = random.randrange(-4, 4)
        apple.y = 10


left_wall = Entity(model="quad",
                color=color.green,
                scale=(0.6, 10),
                position=(-7, 0, 0))
right_wall = duplicate(left_wall, x=7)
randstr = random.randint(1, 10)
apple_color = "ImageBox/RedApple.png"
if (randstr == 1):
    apple_color = "ImageBox/GreenApple.png"
apple = Entity(model="quad",
            texture=apple_color,
            position=(random.randint(-4, 4), 10),
            collider="box")
basket = Entity(model="quad",
                texture="ImageBox/Box.png",
                position=(0, -3.6),
                collider="box")
apple_dy = -2
basket_dx = 5
score = 0
nums = 0
print_on_screen(f"Ready? Go! \n", position=(0, 0), scale=3, origin=(0, 0))
print_on_screen(f"(Press left mouse background music will play again)", position=(0, 0), scale=1.5, origin=(0, 2))
if score == 0:
    print_on_screen(f"You had not Apple and not score.", scale=1, duration=3, position=(-.8, .45))
Audio(sound_file_name="ImageBox/background.mp3")
startc = time.time()
app.run()

两个贴图下期打包发

{{ vote && vote.total.up }}