汽车制造商和抛光制造商被误认为是乌龟的属性。

huangapple go评论67阅读模式
英文:

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(&quot;classic&quot;)
        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() &gt;= 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, &quot;w&quot;)
screen.onkeypress(player.move_bk, &quot;s&quot;)
screen.onkeypress(player.move_left, &quot;a&quot;)
screen.onkeypress(player.move_right, &quot;d&quot;)
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) &lt;= 20:
            game_is_on = False
            scoreboard.gameover()
    if scoreboard.score &gt; 0:
        buff.buffmaker()
    for buff in buff.buffs:
        if player.distance(buff)&lt;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) &lt;= 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(&quot;Gifs/bomb.gif&quot;)
        self.bombs=[]


    def trap_maker(self):
        trap = Turtle()
        trap.shape(&quot;Gifs/bomb.gif&quot;)
        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(&quot;Gifs/stopwatch.gif&quot;)
        self.buffs=[]


    def buffmaker(self):
        buff = Turtle()
        buff.shape(&quot;Gifs/stopwatch.gif&quot;)
        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 = [&quot;red&quot;, &quot;orange&quot;, &quot;yellow&quot;, &quot;green&quot;, &quot;blue&quot;, &quot;purple&quot;]
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(&quot;square&quot;)
            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(&quot;Gifs/stopwatch.gif&quot;)
        self.buffs=[]

    def buffmaker(self):
        buff = Turtle()
        buff.shape(&quot;Gifs/stopwatch.gif&quot;)
        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() &gt;= 300:
        return True
    else:
        False

The simple fix is:

def at_finish_line(self):
    if self.ycor() &gt;= 300:
        return True
    else:
        return False

The better fix is:

def at_finish_line(self):
    return self.ycor() &gt;= 300:

huangapple
  • 本文由 发表于 2023年5月25日 12:10:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定