python - 油罐没有出现
问题描述
我正在使用模块 Pygame zero 在 Python 3.7 中编写游戏。游戏名称为“路怒”。我试图让燃料罐出现在路上以收集它们,但由于某种原因它们没有出现。我试图打印出控制台中有多少个燃料罐,但它甚至没有创建演员。它没有出现任何错误消息,只是没有创建燃料罐。
# Setting the window on center of the screen
import pygame
WIDTH = 720
HEIGHT = 710
pygame.display.set_mode((WIDTH, HEIGHT))
# Importing
from pgzrun import go
from random import choice
WIDTH = 720
HEIGHT = 710
# Variables
TITLE = 'Road Rage'
mode = 'menu'
CAR_IMAGES = ['blue_car', 'pink_car', 'red_car']
road = Actor('road', (WIDTH - 70, HEIGHT), anchor=('right', 'bottom'))
player = Actor('main_car', (WIDTH / 2, HEIGHT))
random_cars = [Actor('red_car', (choice([WIDTH / 2 - 200, WIDTH / 2, WIDTH / 2 + 200]), -250))]
delay = 0.01
fuel_level = 1000
fuel_cans = [Actor('fuel_bag', (choice([WIDTH/2-200, WIDTH/2, WIDTH/2+200]), -250))]
def draw():
global delay
if mode is 'game':
screen.fill((31, 201, 0))
road.draw()
player.draw()
for car in random_cars:
car.draw()
for fuel_bag in fuel_cans:
fuel_bag.draw()
screen.draw.text(str(fuel_level), (20, 20))
elif mode is 'menu':
screen.fill((4, 173, 238))
player.draw()
if player.y > WIDTH / 2:
clock.schedule(up_one_px, delay)
screen.draw.text('ROAD RAGE', center=(WIDTH / 2, 40), fontsize=100, fontname='font')
screen.draw.text('EXPLORE THE TRUE ROAD RAGE AND DRIVE YOUR CAR AROUND THE CITY', center=(WIDTH / 2, 88), fontsize=16, fontname='font')
screen.draw.text('PRESS SPACE TO START THE GAME!',
center=(WIDTH / 2, 150), fontsize=35, fontname='font')
delay += 0.01
def update():
global fuel_level, mode
if mode is 'menu' and player.y < HEIGHT / 2:
player.y = HEIGHT / 2
if mode is 'game':
fuel_level -= 1
if fuel_level <= 0:
mode = 'game over'
for bag in fuel_cans:
for car in random_cars:
while bag.colliderect(car):
bag.x = choice([WIDTH/2-200, WIDTH/2, WIDTH/2+200])
update_road()
update_random_cars()
player.y = HEIGHT / 2 + 200
def up_one_px():
player.y -= 7
def update_road():
road.y += 10
if road.y >= HEIGHT + 600:
road.y = HEIGHT
def new_random_car():
random_cars.append(Actor(choice(CAR_IMAGES), (choice([WIDTH / 2 - 200, WIDTH / 2, WIDTH / 2 + 200]), -250)))
def new_fuel_bag():
fuel_cans.append(Actor('fuel_bag', (choice([WIDTH/2-200, WIDTH/2, WIDTH/2+200]), -250)))
def update_random_cars():
global mode
if random_cars[-1].y >= 400:
new_random_car()
for car in random_cars:
car.y += 10
if car.colliderect(player) or fuel_level <= 0:
mode = 'game over'
elif car.y > WIDTH + 250:
random_cars.remove(car)
def update_fuel_cans():
global fuel_level
if fuel_cans[-1].y >= 400:
new_fuel_bag()
for fuel_bag in fuel_cans:
fuel_bag.y += 10
if fuel_bag.colliderect(player):
fuel_level += 200
elif fuel_bag.y < WIDTH + 250:
fuel_cans.remove(fuel_bag)
def on_key_down(key):
global mode
if mode is 'game':
if key == keys.LEFT:
if player.x == WIDTH / 2 or player.x == WIDTH / 2 + 200:
player.x -= 200
elif key == keys.RIGHT:
if player.x == WIDTH / 2 - 200 or player.x == WIDTH / 2:
player.x += 200
elif mode is 'menu':
if key is keys.SPACE:
mode = 'game'
go()
解决方案
你错过了电话update_fuel_cans
。来电update_fuel_cans
更新:
def update():
global fuel_level, mode
if mode is 'menu' and player.y < HEIGHT / 2:
player.y = HEIGHT / 2
if mode is 'game':
fuel_level -= 1
if fuel_level <= 0:
mode = 'game over'
for bag in fuel_cans:
for car in random_cars:
while bag.colliderect(car):
bag.x = choice([WIDTH/2-200, WIDTH/2, WIDTH/2+200])
update_road()
update_random_cars()
update_fuel_cans() # <--- this is missing
player.y = HEIGHT / 2 + 200
问题是导致文件包被删除的情况:
elif fuel_bag.y < WIDTH + 250: fuel_cans.remove(fuel_bag)
条件总是true
。因此,袋子立即被移除。
可能应该是:
elif fuel_bag.y >= WIDTH:
fuel_cans.remove(fuel_bag)
如果玩家收集了所有的燃料袋,你根本不会创建任何新的燃料袋。fuel_cans
我建议在列表为空时创建一个燃料袋:
def update_fuel_cans():
global fuel_level
if len(fuel_cans) < 1 or fuel_cans[-1].y >= 400:
new_fuel_bag()
for fuel_bag in fuel_cans[:]:
fuel_bag.y += 10
if fuel_bag.colliderect(player):
fuel_level += 200
fuel_cans.remove(fuel_bag)
elif fuel_bag.y >= WIDTH:
fuel_cans.remove(fuel_bag)