Q&A
前提
プログラミング(python)の超初心者です!
ちょっとしたゲームを作ろうとしているのですが、画像の点滅とキャラクターの移動を一緒に動かしたいです。
そのため、ネットなどで色々調べたりしたのですが理解力がなさ過ぎてよくわかりませんでした...
是非わかりやすく教えてくれると幸いです。
実現したいこと
ここに実現したいことを箇条書きで書いてください。
- ▲▲機能を動作するようにする
発生している問題・エラーメッセージ
RuntimeWarning: coroutine 'func1' was never awaited func(*args) RuntimeWarning: Enable tracemalloc to get the object allocation traceback
該当のソースコード
python
1import tkinter 2import asyncio 3import time 4import random 5 6key = "" 7 8 9def key_down(e): 10 global key 11 key = e.keysym 12 13 14def key_up(e): 15 global key 16 key = "" 17 18 19cx = 400 20cy = 300 21 22 23def ran_ran(): 24 r = random.randint(1, 2) 25 if r == 1: 26 accident() 27 if r == 2: 28 accident2() 29 30 31async def func1(z): 32 global cx, cy 33 if key == "w": 34 cy = cy - 1 35 if key == "s": 36 cy = cy + 1 37 if key == "a": 38 cx = cx - 1 39 if key == "d": 40 cx = cx + 1 41 canvas.coords("SHIP", cx, cy) 42 root.after(10, func1) 43 44 45def draw_1(): 46 x = 200 47 y = 400 48 canvas.create_image(x, y, image=img2, tag="acc") 49 50 51def draw_2(): 52 x = 200 53 y = 400 54 canvas.create_image(x, y, image=img3, tag="enemy1") 55 56 57def draw_3(): 58 x = 500 59 y = 300 60 canvas.create_image(x, y, image=img4, tag="enemy2") 61 62 63async def accident(): 64 for i in range(5): 65 draw_1() 66 canvas.update() 67 #time.sleep(0.3) 68 await asyncio.sleep(0.3) 69 canvas.delete("acc") 70 canvas.update() 71 await asyncio.sleep(0.3) 72 draw_2() 73 canvas.update() 74 await asyncio.sleep(3) 75 canvas.delete("enemy1") 76 77 78 79async def accident2(): 80 for i in range(5): 81 draw_1() 82 canvas.update() 83 await asyncio.sleep(0.3) 84 canvas.delete("acc") 85 canvas.update() 86 await asyncio.sleep(0.3) 87 draw_3() 88 canvas.update() 89 await asyncio.sleep(3) 90 canvas.delete("enemy2") 91 92 93 94async def main(): 95 task1 = asyncio.create_task(accident()) 96 task2 = asyncio.create_task(accident()) 97 task3 = asyncio.create_task(func1()) 98 await task1 99 await task2 100 await task3 101 102 103root = tkinter.Tk() 104root.title("避け") 105root.bind("<KeyPress>", key_down) 106root.bind("<KeyRelease>", key_up) 107canvas = tkinter.Canvas(width=800, height=600, bg="blue") 108canvas.pack() 109img = tkinter.PhotoImage(file="ship1.png") 110img2 = tkinter.PhotoImage(file="tyuui.png") 111img3 = tkinter.PhotoImage(file="hosonagaiyatu.png") 112img4 = tkinter.PhotoImage(file="bakemonmon.png") 113canvas.create_image(cx, cy, image=img, tag="SHIP") 114asyncio.run(main()) 115root.mainloop()
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2023/01/05 14:08