[turtle]How would I create gravity? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: [turtle]How would I create gravity? (/thread-18468.html) |
[turtle]How would I create gravity? - GalaxyCoyote - May-19-2019 I want to make a simple platforming game where the player must reach the end, but at the moment I just have 2-way movement (right and left) is there any way I could simulate gravity? Some notes: I am not using pygame. Edit: here is the code I have, import turtle import time pause = 0.1 #screen Screen = turtle.Screen() Screen.title("Block") Screen.bgcolor("white") Screen.setup(width=1280, height=720) Screen.tracer(0)#turns off the screen updates #game Block = turtle.Turtle() Block.speed(0)#no slow down with animation speed Block.shape("square")#shape Block.color("black")#color not colour Block.penup()#stops drawing Block.goto(0,0)#spawns in Block.direction = "stop"#cant you read #fucntions def go_up(): Block.direction = "up" def go_down(): Block.direction = "down" def go_left(): Block.direction = "left" def go_right(): Block.direction = "right" def move(): if Block.direction == "up": y = Block.ycor() Block.sety(y + 20) #time.sleep() elif Block.direction == "down": y = Block.ycor() Block.sety(y - 20) elif Block.direction == "left": x = Block.xcor() Block.setx(x - 20) elif Block.direction == "right": x = Block.xcor() Block.setx(x + 20) #keyboard Screen.onkeypress(go_up, "w") Screen.onkeypress(go_down, "`") Screen.onkeypress(go_left, "a") Screen.onkeypress(go_right, "d") Screen.listen() #gameloop while True: Screen.update() move() time.sleep(pause) Screen.mainloop() RE: How would I create gravity? - ichabod801 - May-19-2019 Just drop the piece a little every turn. You could just put this at the end of the move function: y = Block.ycor() Block.sety(y - 10)Of course, gravity is an acceleration, not just a speed. you could have a variable gravity that starts at -10, decreases by 10 every turn, but increases by 20 (to a max of -10) if you go up. Since you are not using OOP, I would say make that a variable in your while True loop, pass it to move as a parameter, and return the updated value back to the while loop. RE: [turtle]How would I create gravity? - gr3tnosk - May-29-2019 hm, seems you should add gravity, but just as ichabod801 said, it will need to accelerate, but at the same time not let the turtle escape the screen. I haven't worked with pygame, but I've seen a similar problem and solution on stackoverflow: https://stackoverflow.com/questions/16551009/gravity-in-pygame according to this, you would have to determine your initial gravity and just add to it. RE: [turtle]How would I create gravity? - TTChaos - May-29-2019 Do something like: speed_y = 0 while True: speed_y -= 1 y = block.ycor() Block.sety(y+speed_y)Though this would create a problem making the player fall through the floor. Assuming you have a square for a floor(with size 15) right underneath the block: def touch_ground(): while turtle.distance(square) < 15: Block.sety(y+1) speed_y = 0 while True: speed_y -= 1 y = Block.ycor() Block.sety(y+speed_y) touch_ground() RE: [turtle]How would I create gravity? - joe_momma - May-31-2019 Long time ago they had a demo of the earth and moon rotating the sun. it can be found here: https://svn.python.org/projects/python/trunk/Demo/turtle/tdemo_planet_and_moon.py |