Files
PyGameZero/import pgzrun.py
2026-06-15 10:08:51 +08:00

55 lines
1.2 KiB
Python

import pgzrun
import random
WIDTH = 800
HEIGHT = 600
background = Actor('grass')
player = Actor('p3_front')
player.x = 400
player.y = 300
coin = Actor('coingold')
coin.x = random.randint(0, 800)
coin.y = random.randint(0, 600)
score = 0
timer = 10
def update():
global score
global timer
if timer <= 0:
return
timer -= 1 / 60
if keyboard.up:
player.y -= 5
if keyboard.down:
player.y += 5
if keyboard.left:
player.x -= 5
if keyboard.right:
player.x += 5
if player.colliderect(coin):
coin.x = random.randint(0, 800)
coin.y = random.randint(0, 600)
score += 1
def draw():
if timer <= 0:
screen.clear()
screen.draw.text('Game Over', (350,270), color=(255,255,255), fontsize=30)
screen.draw.text('Score: ' + str(score), (350,330), color=(255,255,255), fontsize=30)
else:
background.draw()
player.draw()
coin.draw()
screen.draw.text('Score: ' + str(score), (15,10), color=(255,255,255), fontsize=30)
screen.draw.text('Time: ' + str(round(timer, 2)), (330,10), color=(255,255,255), fontsize=30)
pgzrun.go() # Must be last line