Python Forum
how can I change xcor and ycor of the turtle if i am using classes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how can I change xcor and ycor of the turtle if i am using classes
#1
import turtle
import time

#I have been creating a flappy bird game using turtle module in python
#I have come across a problem where i can't change the xcor and ycor of the pipes 
#please check the class and help me


#creating the screen
win = turtle.Screen()
win.bgpic("C://Users//User//Desktop//flappy bird background.png")
win.tracer(0)

#creating the player
bird = turtle.Turtle()
bird.speed(0)
bird.shape("turtle")
bird.color("red")
bird.penup()
bird.goto(-200, 0)
bird.dx = 0
bird.dy = 1

#creating pipes
class pipe(object):
    def __init__(self, x,y,shape,width, height):
        self.x = x
        self.y = y
        self.shape = shape
        self.width = width
        self.height = height
        pipe = turtle.Turtle()
        pipe.speed(0)
        pipe.shape(self.shape)
        pipe.color("green")
        pipe.shapesize(stretch_wid=self.width, stretch_len=self.height)
        pipe.penup()
        pipe.goto(self.x, self.y)
        pipe.dx = -3
        pipe.dy = 0

object1 = pipe(-100, 200, "square", 10, 2)
object2 = pipe(-100, -200, "square", 18, 2)
object3 = pipe(100, 200, "square", 14, 2)
object4 = pipe(100, -200, "square", 16, 2)

#gravity variable
gravity = -0.9

#functions area
def go_up():
    bird.dy += 10

    if bird.dy > 10:
        bird.dy = 10

#keyboard binding
turtle.listen()
turtle.onkeypress(go_up, "space")

while True:
    time.sleep(0.09)
    win.update()
    bird.forward(2)
    #applying gravity for the game
    bird.dy += gravity

    #move the player
    y = bird.ycor()
    y += bird.dy
    bird.sety(y)

    y = bird.ycor()
    if y < -240:
        y = -240
    bird.sety(y)



win.mainloop()
Reply
#2
What is the problem? If I change the coordinates of the pipes they appear at different locations on the screen.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can i change the direction of the ball based on player's xcor and ycor collisions MohammedSohail 0 1,728 May-25-2020, 08:39 AM
Last Post: MohammedSohail
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 6,124 Feb-06-2019, 01:25 AM
Last Post: woooee
  Help! Turtle not working, even when we click the turtle demo in IDLE nothing happens. BertyBee 3 5,539 Jan-04-2019, 02:44 AM
Last Post: SheeppOSU
  Using classes? Can I just use classes to structure code? muteboy 5 4,978 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020