from tkinter import*
import random
import time
class Ball:
def init(self,canvas,color):
self.canvas=canvas
self.id=canvas.create_oval(10,10,25,25,fill=color)
self.canvas.move(self.id,245,100)
self.canvas_height=self.canvas.winfo_height()
def draw(self): pos=self.canvas.coords(self.id) self.x=0 self.y=1 if pos[1]<=0: self.y=1 if pos[3]>=self.canvas_height: self.y=-1 self.canvas.move(self.id,self.x,self.y)
tk=Tk()
tk.title("GAME")
tk.resizable(0,0)
tk.wm_attributes("-topmost",1)
canvas=Canvas(tk,width=500,height=400,bd=0,highlightthickness=0)
canvas.pack()
ball=Ball(canvas,"red")
while True:
ball.draw()
tk.update_idletasks()
tk.update()
time.sleep(0.01)
ボールを上下に動かしたいのですが、y座標が0のときにボールが固まって動けなくなります。ボールを動かすためにどのようなことをすればよいでしょうか?
ソースコードは全文を記載してください
また、見づらいのでcode表記を使い綺麗にしてください
回答1件
あなたの回答
tips
プレビュー