'AttributeError: 'Turtle' object has no attribute 'create_mountain'
I'm creating a flappy bird style game using the turtle module in PyCharm. Mountains and clouds move across the screen, created from classes. This issue was not here 2 days ago and I feel as though I've tried everything to solve it. Haven't changed the part of the code this applies to, it only appeared after writing an unrelated "if" statement.
Here's the code from main.py file:
from turtle import Screen
from flappybird import Bird
from cloud_manager import Cloud
from mountain_manager import Mountain
from ground_manager import Ground
import time
screen = Screen()
screen.setup(800, 400)
screen.bgcolor("light blue")
screen.title("FloppyBird")
screen.tracer(0)
benny = Bird()
ground = Ground()
mountain = Mountain()
cloud = Cloud()
screen.listen()
screen.onkey(benny.go_up, "Up")
screen.onkey(benny.go_down, "Down")
game_on = True
while game_on:
time.sleep(0.1)
screen.update()
mountain.create_mountain()
mountain.move_mountain()
cloud.create_cloud()
cloud.move_cloud()
and here's the code from cloud_manager.py file:
from turtle import Turtle
import random
MOVE_SPEED = 10
class Cloud:
def __init__(self):
self.all_clouds = []
self.cloud_speed = MOVE_SPEED
def create_cloud(self):
random_chance = random.randint(1, 40)
if random_chance == 1:
new_cloud = Turtle("circle")
new_cloud.color("white")
new_cloud.shapesize(stretch_wid=random.randint(1, 4), stretch_len=random.randint(4, 7))
new_cloud.penup()
random_y = random.randint(-70, 175)
new_cloud.goto(400, random_y)
self.all_clouds.append(new_cloud)
def move_cloud(self):
for cloud in self.all_clouds:
cloud.backward(self.cloud_speed)
It's also not working for the mountain_manager.py file but it's essentially the same code as cloud_manager.py. I'm new here and would appreciate any help. This is my first project not from a tutorial.
edit: added mountain.py file:
from turtle import Turtle
import random
MOVE_SPEED = 10
MOUNTAIN_START = (400, -83)
class Mountain:
def __init__(self):
self.all_mounts = []
self.mountain_speed = MOVE_SPEED
def create_mountain(self):
random_chance = random.randint(1, 40)
if random_chance == 1:
new_mountain = Turtle("triangle")
new_mountain.color("tan")
new_mountain.shapesize(stretch_wid=5, stretch_len=8)
new_mountain.penup()
new_mountain.tilt(90)
new_mountain.goto(MOUNTAIN_START)
self.all_mounts.append(new_mountain)
def move_mountain(self):
for mountain in self.all_mounts:
mountain.backward(self.mountain_speed)
Solution 1:[1]
I reformatted your code into a single file and tossed anything that wasn't applicable to the problem at hand. It runs fine. Your problem is not with the code you are showing us, so consider isolating it to a minimal, workable example (mwe) that displays the error.
Below is my standalone example rework of your code. As I said, it ran fine as it was, but afterward I cleaned it up a bit style-wise:
from turtle import Screen, Turtle
from random import randint
MOVE_SPEED = 10
MOUNTAIN_START = (400, -83)
class Cloud:
def __init__(self):
self.all_clouds = []
self.cloud_speed = MOVE_SPEED
def create_cloud(self):
random_chance = randint(1, 40)
if random_chance == 1:
if self.all_clouds:
new_cloud = self.all_clouds[-1].clone()
else:
new_cloud = Turtle('circle')
new_cloud.color('white')
new_cloud.penup()
new_cloud.shapesize(stretch_wid=randint(1, 4), stretch_len=randint(4, 7))
random_y = randint(-70, 175)
new_cloud.goto(400, random_y)
self.all_clouds.append(new_cloud)
def move_clouds(self):
for cloud in self.all_clouds:
cloud.backward(self.cloud_speed)
class Mountain:
def __init__(self):
self.all_mountains = []
self.mountain_speed = MOVE_SPEED
def create_mountain(self):
random_chance = randint(1, 40)
if random_chance == 1:
if self.all_mountains:
new_mountain = self.all_mountains[-1].clone()
else:
new_mountain = Turtle('triangle')
new_mountain.color('tan')
new_mountain.penup()
new_mountain.tilt(90)
new_mountain.shapesize(stretch_wid=5, stretch_len=8)
new_mountain.goto(MOUNTAIN_START)
self.all_mountains.append(new_mountain)
def move_mountains(self):
for mountain in self.all_mountains:
mountain.backward(self.mountain_speed)
def run():
if game_on:
mountain.create_mountain()
mountain.move_mountains()
cloud.create_cloud()
cloud.move_clouds()
screen.update()
screen.ontimer(run, 100)
screen = Screen()
screen.setup(800, 400)
screen.bgcolor('light blue')
screen.title("FloppyBird")
screen.tracer(0)
mountain = Mountain()
cloud = Cloud()
game_on = True
run()
screen.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | cdlane |
