Python實(shí)現(xiàn)的彈球小游戲示例
本文實(shí)例講述了Python實(shí)現(xiàn)的彈球小游戲。分享給大家供大家參考,具體如下:
彈球
1. Ball 類
draw負(fù)責(zé)移動(dòng)Ball
碰撞檢測(cè),反彈,Ball檢測(cè)Paddle
2.Paddle類
draw負(fù)責(zé)移動(dòng)Paddle
碰撞檢測(cè),確定能不能繼續(xù)
監(jiān)聽鍵盤事件
3.主循環(huán)
繪制Ball和Paddle
update
sleep
代碼
from Tkinter import * import random import time class Ball: def __init__(self, canvas, paddle, color): self.canvas = canvas self.paddle = paddle self.id = canvas.create_oval(10, 10, 25, 25, fill=color) self.canvas.move(self.id, 245, 100) startx = [-3, -2, -1, 1, 2, 3] random.shuffle(startx) self.x = startx[0] self.y = -3 self.canvas_height = self.canvas.winfo_height() self.canvas_width = self.canvas.winfo_width() self.hit_bottom = False def draw(self): self.canvas.move(self.id, self.x, self.y) pos = self.canvas.coords(self.id)#top-left bottom-right if (pos[1] <= 0 or self.hit_paddle(pos) == True): self.y = -self.y if (pos[0] <= 0 or pos[2] >= self.canvas_width): self.x = -self.x if (pos[3] >= self.canvas_height): self.hit_bottom = True 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 True return False class Paddle: def __init__(self, canvas, color): self.canvas = canvas self.id = canvas.create_rectangle(0, 0, 100, 10, fill = color) self.x = 0 self.canvas.move(self.id, 200, 300) self.canvas_width = self.canvas.winfo_width() self.canvas.bind_all("<Key-Left>", self.turn_left) self.canvas.bind_all("<Key-Right>", self.turn_right) def draw(self): pos = self.canvas.coords(self.id) if (pos[0] + self.x >= 0 and pos[2] + self.x <= self.canvas_width): self.canvas.move(self.id, self.x, 0) #self.x = 0 def turn_left(self, event): self.x = -4 def turn_right(self, event): self.x = 4 tk = Tk() tk.title("Game") tk.resizable(0, 0)#not resizable tk.wm_attributes("-topmost", 1)#at top canvas = Canvas(tk, width = 500, height = 500, bd = 0, highlightthickness = 0) canvas.pack() tk.update()#init 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)
運(yùn)行效果如下圖:
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
基于Python實(shí)現(xiàn)語(yǔ)音識(shí)別和語(yǔ)音轉(zhuǎn)文字
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)語(yǔ)音識(shí)別和語(yǔ)音轉(zhuǎn)文字功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-09-09python 解決print數(shù)組/矩陣無(wú)法完整輸出的問題
這篇文章主要介紹了關(guān)于python 解決print數(shù)組/矩陣無(wú)法完整輸出的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02PyQt5+python3+pycharm開發(fā)環(huán)境配置教程
這篇文章主要介紹了PyQt5+python3+pycharm開發(fā)環(huán)境配置教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03python 如何利用argparse解析命令行參數(shù)
這篇文章主要介紹了python 利用argparse解析命令行參數(shù)的步驟,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-09-09Pandas讀取MySQL數(shù)據(jù)到DataFrame的方法
今天小編就為大家分享一篇Pandas讀取MySQL數(shù)據(jù)到DataFrame的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07python中利用numpy.array()實(shí)現(xiàn)倆個(gè)數(shù)值列表的對(duì)應(yīng)相加方法
今天小編就為大家分享一篇python中利用numpy.array()實(shí)現(xiàn)倆個(gè)數(shù)值列表的對(duì)應(yīng)相加方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08