英文:
carmaker and buffmaker mistaken as Turtle's attrubute
问题
player.py:
from turtle import Turtle
STARTING_POSITION = (0, -280)
MOVE_DISTANCE = 10
FINISH_LINE_Y = 280
class Player(Turtle):
def __init__(self):
super().__init__()
self.shape("classic")
self.penup()
self.go_to_start()
self.setheading(90)
self.forward(MOVE_DISTANCE)
def move_forward(self):
self.setheading(90)
self.forward(MOVE_DISTANCE)
def move_backward(self):
self.setheading(90)
self.backward(MOVE_DISTANCE)
def move_left(self):
self.setheading(180)
self.forward(MOVE_DISTANCE)
def move_right(self):
self.setheading(0)
self.forward(MOVE_DISTANCE)
def at_finish_line(self):
if self.ycor() >= 300:
return True
else:
return False
def go_to_start(self):
self.goto(STARTING_POSITION)
main.py:
import time
from turtle import Screen
from player import Player
from car_manager import CarManager
from scoreboard import Scoreboard
from buff import Buff
from trap import Trap
car_manager = CarManager()
scoreboard = Scoreboard()
player = Player()
screen = Screen()
buff = Buff()
trap = Trap()
screen.setup(width=600, height=600)
screen.tracer(0)
screen.onkeypress(player.move_forward, "w")
screen.onkeypress(player.move_backward, "s")
screen.onkeypress(player.move_left, "a")
screen.onkeypress(player.move_right, "d")
screen.listen()
game_is_on = True
while game_is_on:
time.sleep(0.1)
screen.update()
car_manager.car_maker()
car_manager.move_cars(scoreboard.score, 0)
for car in car_manager.cars:
if player.distance(car) <= 20:
game_is_on = False
scoreboard.game_over()
if scoreboard.score > 0:
buff.buff_maker()
for buff_item in buff.buffs:
if player.distance(buff_item) < 20:
car_manager.move_cars(scoreboard.score, 1)
time.sleep(3)
car_manager.move_cars(scoreboard.score, 0)
for trap_item in trap.bombs:
if player.distance(trap_item) <= 20:
game_is_on = False
scoreboard.game_over()
if player.at_finish_line():
player.go_to_start()
scoreboard.update_score()
screen.exitonclick()
trap.py:
from turtle import Turtle
import random
class Trap(Turtle):
def __init__(self):
super().__init__()
self.hideturtle()
self.screen.register_shape("Gifs/bomb.gif")
self.bombs = []
def trap_maker(self):
trap = Turtle()
trap.shape("Gifs/bomb.gif")
trap.penup()
trap.goto(random.randint(-100, 100), random.randint(-200, 250))
self.bombs.append(trap)
buff.py:
from turtle import Turtle
import random
class Buff(Turtle):
def __init__(self):
super().__init__()
self.hideturtle()
self.screen.register_shape("Gifs/stopwatch.gif")
self.buffs = []
def buff_maker(self):
buff = Turtle()
buff.shape("Gifs/stopwatch.gif")
buff.penup()
buff.goto(random.randint(-100, 100), random.randint(-200, 250))
self.buffs.append(buff)
car_manager.py:
from turtle import Turtle
import random
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
STARTING_MOVE_DISTANCE = 2
MOVE_INCREMENT = 2
class CarManager(Turtle):
def __init__(self):
super().__init__()
self.hideturtle()
self.cars = []
def car_maker(self):
if random.randint(1, 24) == 1:
car = Turtle()
car.shape("square")
car.penup()
car.color(random.choice(COLORS))
car.setheading(180)
car.shapesize(1, 2)
car.goto(300, random.randint(-200, 250))
self.cars.append(car)
def move_cars(self, multiplier, buff):
for car in self.cars:
car.forward(STARTING_MOVE_DISTANCE + ((0.5 * multiplier) * MOVE_INCREMENT) - (0.25 * buff))
英文:
Iam trying to build a simple road crossing game using python where the player can pick up buffs and got stuck in a trap. If player picks up a buff then car's movement is going to be slowed. if player got stuck on a trap then player will die.
However when trying to summon the buffs and the traps, python assumed that the buffmaker() and trap_maker() methods as a Turtle attribute Thus raising error. What can I do to fix these problems?
player.py
STARTING_POSITION = (0, -280)
MOVE_DISTANCE = 10
FINISH_LINE_Y = 280
class Player(Turtle):
def __init__(self):
super().__init__()
self.shape("classic")
self.pu()
self.go_to_start()
self.setheading(90)
self.fd(MOVE_DISTANCE)
def move_fd(self):
self.setheading(90)
self.fd(MOVE_DISTANCE)
def move_bk(self):
self.setheading(90)
self.bk(MOVE_DISTANCE)
def move_left(self):
self.setheading(180)
self.fd(MOVE_DISTANCE)
def move_right(self):
self.setheading(0)
self.fd(MOVE_DISTANCE)
def at_finish_line(self):
if self.ycor() >= 300:
return True
else:
False
def go_to_start(self):
self.goto(STARTING_POSITION)```
main.py
import time
from turtle import Screen
from player import Player
from car_manager import CarManager
from scoreboard import Scoreboard
from buff import Buff
from trap import Trap
carmanager = CarManager()
scoreboard = Scoreboard()
player = Player()
screen = Screen()
buff = Buff()
trap = Trap()
screen.setup(width=600, height=600)
screen.tracer(0)
screen.onkeypress(player.move_fd, "w")
screen.onkeypress(player.move_bk, "s")
screen.onkeypress(player.move_left, "a")
screen.onkeypress(player.move_right, "d")
screen.listen()
game_is_on = True
while game_is_on:
time.sleep(0.1)
screen.update()
carmanager.carmaker()
carmanager.car_move(scoreboard.score, 0)
for cars in carmanager.cars:
if player.distance(cars) <= 20:
game_is_on = False
scoreboard.gameover()
if scoreboard.score > 0:
buff.buffmaker()
for buff in buff.buffs:
if player.distance(buff)<20:
carmanager.car_move(scoreboard.score, 1)
time.sleep(3)
carmanager.car_move(scoreboard.score, 0)
for trap in trap.bombs:
if player.distance(trap) <= 20:
game_is_on = False
scoreboard.gameover()
if player.at_finish_line():
player.go_to_start()
scoreboard.update_points()
screen.exitonclick()
trap.py
import random
class Trap(Turtle):
def __init__(self):
super().__init__()
self.hideturtle()
self.screen.register_shape("Gifs/bomb.gif")
self.bombs=[]
def trap_maker(self):
trap = Turtle()
trap.shape("Gifs/bomb.gif")
trap.pu()
trap.goto(random.randint(-100, 100), random.randint(-200, 250))
self.bombs.append(trap)```
buff.py
from turtle import Turtle
import random
class Trap(Turtle):
def __init__(self):
super().__init__()
self.hideturtle()
self.screen.register_shape("Gifs/stopwatch.gif")
self.buffs=[]
def buffmaker(self):
buff = Turtle()
buff.shape("Gifs/stopwatch.gif")
buff.pu()
buff.goto(random.randint(-100, 100), random.randint(-200, 250))
self.bombs.append(buff)
carmanager.py
from turtle import Turtle
import random
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
STARTING_MOVE_DISTANCE = 2
MOVE_INCREMENT = 2
class CarManager(Turtle):
def __init__(self):
super().__init__()
self.hideturtle()
self.cars=[]
def carmaker(self):
if random.randint(1, 24)==1:
car = Turtle()
car.shape("square")
car.pu()
car.color(random.choice(COLORS))
car.setheading(180)
car.shapesize(1, 2)
car.goto(300, random.randint(-200, 250))
self.cars.append(car)
def car_move(self, multiplier, buff):
for cars in self.cars:
cars.fd(STARTING_MOVE_DISTANCE + ((0.5*multiplier)*MOVE_INCREMENT )- (0.25*buff))
答案1
得分: 1
我看到的主要问题是Buff
类没有定义,错误地被称为Trap
,并附加到bombs
而不是buffs
:
buff.py
from turtle import Turtle
import random
class Trap(Turtle):
def __init__(self):
super().__init__()
self.hideturtle()
self.screen.register_shape("Gifs/stopwatch.gif")
self.buffs=[]
def buffmaker(self):
buff = Turtle()
buff.shape("Gifs/stopwatch.gif")
buff.pu()
buff.goto(random.randint(-100, 100), random.randint(-200, 250))
self.buffs.append(buff)
看起来是复制粘贴错误。此外,这个函数有点错误:
def at_finish_line(self):
if self.ycor() >= 300:
return True
else:
False
简单的修复是:
def at_finish_line(self):
if self.ycor() >= 300:
return True
else:
return False
更好的修复是:
def at_finish_line(self):
return self.ycor() >= 300
英文:
The primary issue I see is that the class Buff
isn't defined, by mistake it's also called Trap
and appends to bombs
instead of buffs
:
buff.py
from turtle import Turtle
import random
class Trap(Turtle):
def __init__(self):
super().__init__()
self.hideturtle()
self.screen.register_shape("Gifs/stopwatch.gif")
self.buffs=[]
def buffmaker(self):
buff = Turtle()
buff.shape("Gifs/stopwatch.gif")
buff.pu()
buff.goto(random.randint(-100, 100), random.randint(-200, 250))
self.bombs.append(buff)
Looks like a copy and paste error. Also this function is slightly broken:
def at_finish_line(self):
if self.ycor() >= 300:
return True
else:
False
The simple fix is:
def at_finish_line(self):
if self.ycor() >= 300:
return True
else:
return False
The better fix is:
def at_finish_line(self):
return self.ycor() >= 300:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论