Pythonでtkinterを使い縦スクロールゲームを作っています。
プレイヤーの操作する車が障害物に当たるとゲームの画面が停止するようにしたいのですが、after_cancelメソッドを使ってみても画面が動き続けてしまいます。どのようにしたらいいのでしょうか。
Python
1import tkinter 2import numpy as np 3 4px = 150 5py = 550 6bgy = 0 7ox_arg = np.random.randint(0,5) 8ox = 60 * ox_arg + 30 9oy = 0 10ot = 0 11bx_arg = np.random.randint(0,4) 12bx = 60 * bx_arg + 60 13by = -300 14bt = -60 15s = 5 16key = "" 17 18def key_down(e): 19 global key 20 key = e.keysym 21 22def key_up(e): 23 global key 24 key = "" 25 26def main(): 27 global key,roop_id 28 scroll() 29 obstacle() 30 barricade() 31 move() 32 animation() 33 col_detection() 34 roop_id = root.after(50, main) 35 36def animation(): 37 global px,py 38 canvas.create_image(px, py, image = player, tag = "PLAYER") 39 40def move(): 41 global px,py,key 42 canvas.delete("PLAYER") 43 if key == "Left" and px > 30: 44 45 px = px - 10 46 47 if key == "Right" and px < 270: 48 49 px = px + 10 50 51def scroll(): 52 global bgy,s 53 bgy = (bgy + s) % 600 54 55 canvas.delete("BGIMAGE") 56 canvas.create_image(150, bgy + 300, image = bg, tag = "BGIMAGE") 57 canvas.create_image(150, bgy - 300, image = bg, tag = "BGIMAGE") 58 59def obstacle(): 60 global ox,oy,ot,s 61 oy = oy + s 62 ot = ot + 1 63 s = s + 0.01 64 if ot > 600 / s + 10: 65 def_ox() 66 ot = 0 67 oy = 0 68 else: 69 canvas.delete("OBSTACLE") 70 canvas.create_image(ox, oy, image = obs, tag = "OBSTACLE") 71 72def barricade(): 73 global bx,by,bt,s 74 by = by + s 75 bt = bt + 1 76 if bt > 600 / s + 10: 77 def_bx() 78 bt = 0 79 by = 0 80 else: 81 canvas.delete("BARRICADE") 82 canvas.create_image(bx, by,image = bar, tag = "BARRICADE") 83 84def def_ox(): 85 global ox_arg,ox 86 ox_arg = np.random.randint(0,5) 87 ox = 60 * ox_arg + 30 88 89def def_bx(): 90 global bx_arg,bx 91 bx_arg = np.random.randint(0,4) 92 bx = 60 * bx_arg +60 93 94def col_detection(): 95 if abs(ox - px) < 45 and abs(oy - py) < 58: 96 gameover() 97 elif abs(bx - px) < 75 and abs(by - py) <60: 98 gameover() 99 else: 100 pass 101 102def gameover(): 103 global roop_id 104 root.after_cancel(roop_id) 105 106 107root = tkinter.Tk() 108root.title("") 109root.bind("<KeyPress>",key_down) 110root.bind("<KeyRelease>",key_up) 111canvas = tkinter.Canvas(width=300,height=600) 112canvas.pack() 113bg = tkinter.PhotoImage(file="road.png") 114player = tkinter.PhotoImage(file="car1.png") 115obs = tkinter.PhotoImage(file="rock.png") 116bar = tkinter.PhotoImage(file="barricade.png") 117main() 118root.mainloop()
開発環境:Python 3.9.5 pygame 2.0.1