Python Forum
[turtle]How would I create gravity?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[turtle]How would I create gravity?
#1
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()
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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/1655...-in-pygame

according to this, you would have to determine your initial gravity and just add to it.
Reply
#4
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()
Reply
#5
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/t...nd_moon.py
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Turtle]How to make smooth moving and add gravity? ZakHacks 2 6,367 Aug-25-2019, 02:27 AM
Last Post: SheeppOSU
  How to simulate gravity? hsunteik 4 9,205 Feb-03-2017, 07:32 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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