国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

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

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)簡單反彈球游戲

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

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 凤凰县| 阿城市| 桐庐县| 英超| 安溪县| 建德市| 包头市| 周宁县| 上栗县| 凌云县| 塘沽区| 廉江市| 睢宁县| 和顺县| 班戈县| 许昌县| 江门市| 和田市| 宝坻区| 盱眙县| 巴林左旗| 新蔡县| 江华| 普宁市| 泰安市| 萨迦县| 崇文区| 盐山县| 博罗县| 洱源县| 姚安县| 南昌市| 鲁甸县| 濉溪县| 娄烦县| 昆明市| 上蔡县| 枣阳市| 阿图什市| 南充市| 如皋市|