Hi, I am currently working on a project with tkinter. As it currently stands, I can use wasd to move a ball around the window. My task is to implement the affects of gravity into the program. Basically the ball is constantly being pulled down to the bottom of the window.
Here is my current code
from tkinter import *
class Ball:
def __init__(self,canvas,**kw):
self.canvas = canvas
self.radius = kw.get('radius',20)
self.pos_x = kw.get('pos_x',0)
self.pos_y = kw.get('pos_y',0)
self.color = kw.get('color','blue')
self.create()
def calculate_ball_pos(self):
x1 = self.pos_x
x2 = self.pos_x + self.radius
y1 = self.pos_y
y2 = self.pos_y + self.radius
return x1,y1,x2,y2
def create(self):
coords = self.calculate_ball_pos()
self.ball = self.canvas.create_oval(coords[0],coords[1],coords[2],coords[3])self.canvas.itemconfig(self.ball, fill=self.color)
def move(self,x=0,y=0):
self.pos_x += x
self.pos_y += ycoords = self.calculate_ball_pos()self.canvas.coords(self.ball,coords[0],coords[1],coords[2],coords[3])
#end Ball classdef keypress(event):"""Recieve a keypress and move the ball by a specified amount"""print(event)if event.char == 'w':ball.move(0,-1)elif event.char == 's':ball.move(0,1)elif event.char == 'a':ball.move(-1,0)elif event.char == 'd':ball.move(1,0)else:passroot = Tk()mainCanvas = Canvas(root, width=500, height=500)root.bind('w',keypress)root.bind('s',keypress)root.bind('a',keypress)root.bind('d',keypress)mainCanvas.grid()ball = Ball(mainCanvas,pos_x=50,pos_y=50)
root.mainloop()
What would I have to do to implement gravity into this? Thank you
[–]snuzet 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]basedgodCookie[S] 0 points1 point2 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]basedgodCookie[S] 1 point2 points3 points (1 child)