久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

python實現(xiàn)簡單反彈球游戲

瀏覽:20日期:2022-06-22 16:35:39

python簡單游戲-反彈球,供大家參考,具體內容如下

tkinter實現(xiàn),直接貼上代碼

from tkinter import*import timeimport randomclass Ball: def __init__(self,canvas,paddle,color):self.canvas = canvasself.paddle = paddleself.id = canvas.create_oval(10,10,25,25,fill=color)self.canvas.move(self.id,245,100)starts = [-3,-2,-1,1,2,3]random.shuffle(starts)self.x = starts[0]self.y = -3self.canvas_height = self.canvas.winfo_height()self.canvas_width = self.canvas.winfo_width()self.hit_bottom = False def hit_paddle(self,pos):paddle_pos=self.canvas.coords(self.paddle.id)if pos[2]>=paddle_pos[0] and pos[0]<=paddle_pos[2]: if pos[3]>=paddle_pos[1] and pos[3]<=paddle_pos[3]:return Truereturn False def draw(self):self.canvas.move(self.id,self.x,self.y)pos = self.canvas.coords(self.id)if pos[1] <= 0: self.y = 4if pos[3] >= self.canvas_height: self.hit_bottom=Trueif self.hit_paddle(pos)==True: self.y=-4if pos[0] <= 0: self.x = 4if pos[2] >= self.canvas_width: self.x = -4class Paddle: def __init__(self,canvas,color):self.canvas = canvasself.id = canvas.create_rectangle(0,0,100,10,fill=color)self.canvas.move(self.id,200,400)self.x=0self.canvas_width = self.canvas.winfo_width()canvas.bind_all(’<KeyPress-Left>’,self.turn_left)canvas.bind_all(’<KeyPress-Right>’,self.turn_right)self.hit_bottom = False def draw(self):self.canvas.move(self.id,self.x,0)pos = self.canvas.coords(self.id)if pos[0] <= 0: self.x = 0elif pos[2] >= self.canvas_width: self.x = 0 def turn_left(self,evt):self.x=-7 def turn_right(self,evt):self.x=7 tk = Tk()tk.title('反彈吧!球球')#tk.resizable(0,0)tk.wm_attributes('-topmost',1)canvas = Canvas(tk,width=650,height=600,bd=0,highlightthickness=0)canvas.pack()tk.update()paddle=Paddle(canvas,’blue’)ball = Ball(canvas,paddle,’red’)while 1: if ball.hit_bottom==False:ball.draw()paddle.draw() tk.update_idletasks() tk.update() time.sleep(0.01)

效果:

python實現(xiàn)簡單反彈球游戲

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 井陉县| 罗定市| 呼和浩特市| 深州市| 武陟县| 定西市| 台南市| 怀来县| 北宁市| 临泽县| 沅陵县| 竹溪县| 常山县| 股票| 天祝| 襄樊市| 九龙县| 寿宁县| 吴堡县| 新密市| 绩溪县| 六枝特区| 永春县| 榆林市| 浦东新区| 什邡市| 龙门县| 宁蒗| 南陵县| 枣庄市| 夹江县| 永宁县| 临潭县| 黄大仙区| 锦屏县| 中山市| 尚义县| 平果县| 鲁山县| 宜兴市| 高州市|