Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 [Turtle]How to make smooth moving and add gravity? ZakHacks Unladen Swallow Posts: 1 Threads: 1 Joined: Aug 2019 Reputation: 0 Likes received: 0 #1 Aug-24-2019, 01:25 PM (This post was last modified: Aug-24-2019, 02:06 PM by Yoriz. Edited 3 times in total. Edit Reason: Added code tags, Prefix ) Hello, I am new to python coding and I figured this would be a good place to start. I am currently making a Python Turtle Platforming Game and I am not sure how to make the moving smooth and how to add gravity. Here is the code: (The mapping is 25x25 but the spaces don't show up) ```import turtle import math import random wn = turtle.Screen() wn.bgcolor("black") wn.title("Maze Game") wn.setup(1400, 700) wn.tracer(0) class Pen(turtle.Turtle): def __init__(self): turtle.Turtle.__init__(self) self.shape("square") self.color("white") self.penup() self.speed(0) class Player(turtle.Turtle): def __init__(self): turtle.Turtle.__init__(self) self.shape("square") self.color("blue") self.penup() self.speed(0) self.gold = 0 def go_up(self): move_to_x = player.xcor() move_to_y = player.ycor() + 120 if (move_to_x, move_to_y) not in walls or (move_to_y + 120 < 288): self.goto(move_to_x, move_to_y) def go_down(self): move_to_x = player.xcor() move_to_y = player.ycor() - 24 if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) def go_left(self): move_to_x = player.xcor() - 24 move_to_y = player.ycor() def touch_ground(): while turtle.distance(wall) < 24: player.sety(y+1) speed_y = 0 if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) def go_right(self): move_to_x = player.xcor() + 24 move_to_y = player.ycor() if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) def is_collision(self, other): a = self.xcor()-other.xcor() b = self.ycor()-other.ycor() distance = math.sqrt((a ** 2) + (b ** 2)) if distance < 5: return True else: return False levels = [""] level_1 = [ "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "X X", "X XXXX", "X X X", "X XXXXXX X", "X X", "X X", "X X", "XP XXXXXXXX X", "XXXXXXXXXX X", "X X", "X X", "X X", "X XXX X", "X X", "X X", "X X", "X XXXXXXXXXX X", "X X", "X XXXXXXXX X", "X X", "X XXXXXXXXX X", "X XXXXXXXXXXX X", "X X", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", ] treasures = [] levels.append(level_1) def setup_maze(level): for y in range(len(level)): for x in range(len(level[y])): character = level[y][x] screen_x = -588 + (x * 24) screen_y = 288 - (y * 24) if character == "X": pen.goto(screen_x, screen_y) pen.shape("square") pen.stamp() walls.append((screen_x, screen_y)) if character == "P": player.goto(screen_x, screen_y) if character == "T": treasures.append(Treasure(screen_x, screen_y)) pen = Pen() player = Player() walls = [] setup_maze(levels[1]) turtle.listen() turtle.onkey(player.go_left, "Left") turtle.onkey(player.go_right, "Right") turtle.onkey(player.go_up, "Up") turtle.onkey(player.go_down, "Down") wn.tracer(0) speed_y = 0 while True: for treasure in treasures: if player.is_collision(treasure): player.gold += treasure.gold print ("Player Gold:{}".format(player.gold)) treasure.destroy() treasures.remove(treasure) wn.update()``` Yoriz wrote Aug-24-2019, 02:06 PM:Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time. ThomasL Minister of Silly Walks Posts: 361 Threads: 5 Joined: Jun 2019 Reputation: 63 Likes received: 84 #2 Aug-24-2019, 05:01 PM I myself didn´t look into the Turtle module so far. I played some minutes with it and below are my suggestions how smoother movement could be done. You need to look into Python classes, definition, methods and instances. You don´t use the instance player of the class Player in the methods. This is done by using self. Maybe have a read here realpython.com instance class and methods demystified ```class Pen(turtle.Turtle): def __init__(self): # turtle.Turtle.__init__(self) super().__init__() self.shape("square") self.color("white") self.penup() self.speed(0) class Player(turtle.Turtle): def __init__(self): #turtle.Turtle.__init__(self) super().__init__() self.shape("square") self.color("blue") self.penup() self.speed(0) self.gold = 0 def go_up(self): if (self.xcor(), self.ycor()+120) not in walls: for step in range(120): self.goto(self.xcor(), self.ycor()+1) wn.update() def go_down(self): if (self.xcor(), self.ycor()-24) not in walls: for step in range(24): self.goto(self.xcor(), self.ycor()-1) time.sleep(0.010) wn.update() def go_right(self): if (self.xcor()+24, self.ycor()) not in walls: for step in range(24): self.goto(self.xcor()+1, self.ycor()) time.sleep(0.010) wn.update() def go_left(self): if (self.xcor()-24, self.ycor()) not in walls: for step in range(24): self.goto(self.xcor()-1, self.ycor()) time.sleep(0.010) wn.update() def touch_ground(): while turtle.distance(wall) < 24: self.sety(y+1) speed_y = 0 if (move_to_x, move_to_y) not in walls: self.goto(move_to_x, move_to_y) def is_collision(self, other): a = self.xcor()-other.xcor() b = self.ycor()-other.ycor() distance = math.sqrt((a ** 2) + (b ** 2)) if distance < 5: return True else: return False ``` SheeppOSU Minister of Silly Walks Posts: 395 Threads: 70 Joined: Feb 2018 Reputation: 16 Likes received: 40 #3 Aug-25-2019, 02:27 AM (Aug-24-2019, 01:25 PM)ZakHacks Wrote: how to add gravity. To add physics to a game, you just have to look at Newton's laws of motion. For things like gravity and collision, his 3rd law of Action-Reaction explains it best. For every action there is an equal but opposite reaction. What this means is that for every collision the player has with an object, the object pushes back. If you wanted to be really complex you can give each object a maxForce, if something pushes on the object with more than it's maxForce, it will get pushed by the object colliding with it. If the player is touching the ground, it is applying the amount of force that gravity is pulling down with. If the player jumps, they build up force when coming down and so it would be the force gravity is applying + the force it gains while falling (I don't know how to calculate that so you can look it up). If that's more complex than you need, just make gravity pull the player down by 1 pixel each game tick and the ground pushes up on the player 1 pixel each game tick IF the player is touching the ground. Hope this helps! Age Doesn't Matter, Your Passion Does - Shreya Jain « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post [turtle]How would I create gravity? GalaxyCoyote 4 1,295 May-31-2019, 08:34 PM Last Post: joe_momma Can't make the turtle walk.. Turry 3 1,850 Nov-26-2017, 05:05 PM Last Post: heiner55 How to simulate gravity? hsunteik 4 5,234 Feb-03-2017, 07:32 PM Last Post: nilamo

Forum Jump:

Users browsing this thread: 1 Guest(s)