add trees

This commit is contained in:
2026-06-23 15:38:36 +08:00
parent a0755a9376
commit 0934806ac9
3 changed files with 83 additions and 85 deletions

BIN
images/tree.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

View File

@@ -1,57 +0,0 @@
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 = 35
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
sounds.sfx_coin_single1.play()
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

111
main.py
View File

@@ -1,50 +1,105 @@
"""
renny
"""
import pgzrun import pgzrun
import random import random
WIDTH = 800 WIDTH = 800
HEIGHT = 600 HEIGHT = 600
ship = Actor('playership1_blue') background = Actor('grass')
ship.x = 370
ship.y = 550
gem = Actor('gemgreen')
gem.x = random.randint(20, 780) player = Actor('p3_front')
gem.y = 0 player.x = 400
player.y = 300
coin = Actor('coingold')
coin.x = random.randint(50, WIDTH - 50)
coin.y = random.randint(50, HEIGHT - 50)
trees = []
tree_count = 2
for _ in range(tree_count):
new_tree = Actor('tree')
new_tree.x = random.randint(80, WIDTH - 80)
new_tree.y = random.randint(80, HEIGHT - 80)
while new_tree.colliderect(player):
new_tree.x = random.randint(80, WIDTH - 80)
new_tree.y = random.randint(80, HEIGHT - 80)
trees.append(new_tree)
score = 0 score = 0
game_over = False timer = 35
def on_mouse_move(pos, rel, buttons):
ship.x = pos[0] def check_tree_collision(new_x, new_y):
temp_player = Actor('p3_front', pos=(new_x, new_y))
for tree in trees:
if temp_player.colliderect(tree):
return False
return True
def update(): def update():
global score, game_over global score
global timer
if timer <= 0:
return
timer -= 1 / 60
move_speed = 5
if keyboard.up:
new_y = player.y - move_speed
if check_tree_collision(player.x, new_y):
player.y = new_y
if keyboard.down:
new_y = player.y + move_speed
if check_tree_collision(player.x, new_y):
player.y = new_y
if keyboard.left: if keyboard.left:
ship.x = ship.x - 5 new_x = player.x - move_speed
if check_tree_collision(new_x, player.y):
player.x = new_x
if keyboard.right: if keyboard.right:
ship.x = ship.x + 5 new_x = player.x + move_speed
if check_tree_collision(new_x, player.y):
player.x = new_x
if player.colliderect(coin):
while True:
coin.x = random.randint(50, WIDTH - 50)
coin.y = random.randint(50, HEIGHT - 50)
overlap_tree = False
for t in trees:
if coin.colliderect(t):
overlap_tree = True
break
if not overlap_tree:
break
score += 1
sounds.sfx_coin_single1.play()
gem.y = gem.y + 4 + score / 5
if gem.y > 600:
game_over = True
if gem.colliderect(ship):
gem.x = random.randint(20, 780)
gem.y = 0
score = score + 1
def draw(): def draw():
screen.fill((80,0,70)) if timer <= 0:
if game_over: screen.fill((30, 30, 30))
screen.draw.text('Game Over', (360, 300), color=(255,255,255), fontsize=60) screen.draw.text('Game Over', (330, 270), color=(255,255,255), fontsize=36)
screen.draw.text('Score: ' + str(score), (360, 350), color=(255,255,255), fontsize=60) screen.draw.text('Final Score: ' + str(score), (330, 330), color=(255,255,255), fontsize=30)
else: else:
gem.draw() background.draw()
ship.draw()
for tree in trees:
tree.draw()
player.draw()
coin.draw()
screen.draw.text('Score: ' + str(score), (15,10), color=(255,255,255), fontsize=30) 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 pgzrun.go() # Must be last line