This is my platforming game which I built in 2017. It is quite simple, and uses the following method to replicate gravity.
Fall by drop speed If I am touching platforms: Work out which way I am travelling and go in the opposite direction until I am not touching it. Else: Increase drop speed.
Repl.it cannot simulate Tkinter, so just copy this into python and it should run. Try adding levels in create_platforms
. The x is the level number, coords1 and 2 act as x1 and y1 for the rectangle, and 3 and 4 x2 and y2.
from tkinter import * yfrom tkinter.font import Font from time import * window = Tk() c = Canvas(window, width=800, height=500, bg='yellow') c.pack() player = c.create_polygon(90, 0, 90, 20, 110, 20, 110, 0, fill='purple') yv = 0 coords1 = list() coords2 = list() coords3 = list() coords4 = list() recs=[] end=False lv=0 arrows=[False,False,False] def kill(): global end end=True def create_platforms(x): global coords1,coords2,coords3,coords4,recs coords1 = [] coords2 = [] coords3 = [] coords4 = [] for i in recs: c.delete(i) if x == 0: coords1.append(90) coords2.append(300) coords3.append(300) coords4.append(500) coords1.append(300) coords2.append(270) coords3.append(500) coords4.append(500) coords1.append(550) coords2.append(270) coords3.append(750) coords4.append(500) if x == 1: coords1.append(0) coords2.append(0) coords3.append(90) coords4.append(500) coords1.append(90) coords2.append(400) coords3.append(120) coords4.append(500) coords1.append(200) coords2.append(400) coords3.append(300) coords4.append(500) coords1.append(400) coords2.append(350) coords3.append(500) coords4.append(500) coords1.append(600) coords2.append(300) coords3.append(700) coords4.append(350) coords1.append(600) coords2.append(450) coords3.append(800) coords4.append(500) if x == 2: coords1.append(90) coords2.append(300) coords3.append(110) coords4.append(500) coords1.append(200) coords2.append(270) coords3.append(210) coords4.append(500) coords1.append(300) coords2.append(400) coords3.append(310) coords4.append(500) coords1.append(400) coords2.append(350) coords3.append(410) coords4.append(500) coords1.append(500) coords2.append(300) coords3.append(510) coords4.append(500) coords1.append(600) coords2.append(250) coords3.append(800) coords4.append(800) if x == 3: c.create_text(400,250,text="You win!",fill="darkblue",font=Font(size=50)) window.after(5000,kill) coords1.append(0) coords2.append(450) coords3.append(800) coords4.append(500) coords1.append(0) coords2.append(0) coords3.append(50) coords4.append(500) coords1.append(750) coords2.append(0) coords3.append(800) coords4.append(500) for i in range(len(coords1)): recs.append(c.create_rectangle(coords1[i], coords2[i], coords3[i], coords4[i], fill='darkblue', outline="darkblue")) def gravity(): global yv,c,player c.move(player, 0, yv) if touching_platforms(): if yv < 0: rev = 0.1 else: rev = -0.1 while touching_platforms(): c.move(player, 0, rev) yv = 0 else: yv += 0.2 def touching_platforms(): crds = c.coords(player) y = (crds[1] + crds[3])/2 x = (crds[0] + crds[2])/2 for i in range(len(coords1)): if touching_block(coords1[i], coords2[i], coords3[i], coords4[i], x, y): return True return False def move_player(): global yv if arrows[0]: c.move(player, -4, 0) while touching_platforms(): c.move(player, 1, 0) if arrows[1]: c.move(player, 4, 0) while touching_platforms(): c.move(player, -1, 0) if arrows[2]: c.move(player, 0, 1) if touching_platforms(): yv=-5 c.move(player, 0, -1) def handleon(event): if event.keysym == 'Left': arrows[0]=True if event.keysym == 'Right': arrows[1]=True if event.keysym == 'Up': arrows[2]=True def handleoff(event): if event.keysym == 'Left': arrows[0]=False if event.keysym == 'Right': arrows[1]=False if event.keysym == 'Up': arrows[2]=False def dead(): global yv,c if c.coords(player)[1]>500: x=c.create_rectangle(0,0,1000,500,fill="red") c.pack() window.update() sleep(0.1) c.delete(x) yv=0 c.move(player,100-c.coords(player)[0],-c.coords(player)[1]) def won(): global yv,c,lv if c.coords(player)[0]>800: x=c.create_rectangle(0,0,1000,500,fill="green") c.pack() window.update() sleep(0.1) c.delete(x) yv=0 lv+=1 create_platforms(lv) c.move(player,100-c.coords(player)[0],-c.coords(player)[1]) c.bind_all('<KeyPress>', handleon) c.bind_all('<KeyRelease>', handleoff) def touching_block(x1, y1, x2, y2, px, py): return (x1<px+20 and x2>px) and (y1<py+10 and y>py-10) create_platforms(0) while True: gravity() c.pack() move_player() dead() won() window.update() sleep(0.01) if end: break