Hier ist mein code:
Code: Alles auswählen
#tkinter
from tkinter import *
from random import *
from time import *
screen = Tk()
screen.title('jump and run')
c = Canvas(screen, width=1400, height=700, bg='blue')
c.pack()
Level = 1
runter = 0
Plate_id = list()
levelend = c.create_rectangle(1400, 700, 2000, 710)
#Player
Player1 = c.create_rectangle(100, 640, 120, 690, fill='red')
Player2 = c.create_rectangle(100, 640, 120, 690, fill='green')
c.itemconfig(Player2, state=HIDDEN)
#Plate
def screen_plate(PX, PY, Länge):
PL = c.create_rectangle(PX, PY, PX + Länge, PY - 10, fill='lime')
Plate_id.append(PL)
class schwerkraft:
def coords(ob):
pos = c.coords(ob)
x1 = pos[0]
y1 = pos[1]
x2 = pos[2]
y2 = pos[3]
return x1, y1, x2, y2
def check(px1, py1, px2, py2, cx1, cy1, cx2, cy2):
if py2 == cy1:
if px1 > cx1 and px1 < cx2:
colision = 1
elif px2 < cx2 and px2 > cx1:
colision = 1
else:
colision = 0
else:
colision = 0
return colision
def sprung(objekt, P2):
haupt_colision = 0
colision2 = 0
px1, py1, px2, py2 = schwerkraft.coords(objekt)
for I in range(len(Plate_id)-1, -1, -1):
cx1, cy1, cx2, cy2 = schwerkraft.coords(Plate_id[I])
colision = schwerkraft.check(px1, py1, px2, py2, cx1, cy1, cx2, cy2)
if colision == 1:
haupt_colision = 1
if colision == 0:
pass
if haupt_colision == 1:
for i in range(80):
c.move(objekt, 0, -1)
c.move(P2, 0, -1)
screen.update()
sleep(0.01)
while colision2 == 0:
c.move(objekt, 0, 1)
c.move(P2, 0, 1)
screen.update()
sleep(0.01)
px1, py1, px2, py2 = schwerkraft.coords(objekt)
Levelcheck(levelend, Player1)
for I in range(len(Plate_id)-1, -1, -1):
cx1, cy1, cx2, cy2 = schwerkraft.coords(Plate_id[I])
haupt_colision2 = schwerkraft.check(px1, py1, px2, py2, cx1, cy1, cx2, cy2)
if haupt_colision2 == 0:
pass
if haupt_colision2 == 1:
colision2 = 1
if py1 > 705:
c.coords(objekt, 100, 630, 120, 680)
c.coords(P2, 100, 630, 120, 680)
return 0
def runter(objekt, P2):
haupt_colision = 1
colision2 = 0
px1, py1, px2, py2 = schwerkraft.coords(objekt)
for I in range(len(Plate_id)-1, -1, -1):
cx1, cy1, cx2, cy2 = schwerkraft.coords(Plate_id[I])
colision = schwerkraft.check(px1, py1, px2, py2, cx1, cy1, cx2, cy2)
if colision == 1:
haupt_colision = 0
if colision == 0:
pass
if haupt_colision == 1:
while colision2 == 0:
c.move(objekt, 0, 1)
c.move(P2, 0, 1)
screen.update()
sleep(0.01)
px1, py1, px2, py2 = schwerkraft.coords(objekt)
Levelcheck(levelend, Player1)
for I in range(len(Plate_id)-1, -1, -1):
cx1, cy1, cx2, cy2 = schwerkraft.coords(Plate_id[I])
haupt_colision2 = schwerkraft.check(px1, py1, px2, py2, cx1, cy1, cx2, cy2)
if haupt_colision2 == 0:
pass
if haupt_colision2 == 1:
colision2 = 1
if py1 > 705:
c.coords(objekt, 100, 630, 120, 680)
c.coords(P2, 100, 630, 120, 680)
def Levelup():
if Level == 1:
screen_plate(0, 700, 1400)
screen_plate(200, 630, 30)
if Level == 2:
screen_plate(0, 700, 1400)
screen_plate(500, 630, 30)
if Level == 3:
screen_plate(0, 700, 1400)
screen_plate(1000, 630, 30)
def Levelcheck(levelend, Player):
global Level
px1, py1, px2, py2 = schwerkraft.coords(Player)
cx1, cy1, cx2, cy2 = schwerkraft.coords(levelend)
Levelcheck = schwerkraft.check(px1, py1, px2, py2, cx1, cy1, cx2, cy2)
if Levelcheck == 1:
for I in range(len(Plate_id)-1, -1, -1):
c.delete(Plate_id[I])
del Plate_id[:]
Level += 1
Levelup()
print('levelup')
def move(event):
global runter
if event.keysym == 'Up':
if runter != 1:
runter = 1
runter = schwerkraft.sprung(Player1, Player2)
elif event.keysym == 'Right':
c.move(Player1, 10, 0)
c.move(Player2, 10, 0)
c.itemconfig(Player1, state=NORMAL)
c.itemconfig(Player2, state=HIDDEN)
Levelcheck(levelend, Player1)
if runter == 0:
schwerkraft.runter(Player1, Player2)
elif event.keysym == 'Left':
c.move(Player1, -10, 0)
c.move(Player2, -10, 0)
c.itemconfig(Player1, state=HIDDEN)
c.itemconfig(Player2, state=NORMAL)
Levelcheck(levelend, Player1)
if runter == 0:
schwerkraft.runter(Player1, Player2)
c.bind_all('<Key>', move)
Levelup()
PS: die Level sind noch nicht fertig.
PPS: bitte nicht einfach nur sagen was ich falsch mache sondern wenn dann auch alternativen liefern.