python實(shí)現(xiàn)簡單反彈球游戲
更新時(shí)間:2021年04月12日 11:18:19 作者:匿名用戶__
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡單反彈球游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
python簡單游戲-反彈球,供大家參考,具體內(nèi)容如下
tkinter實(shí)現(xiàn),直接貼上代碼
from tkinter import*
import time
import random
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)
starts = [-3,-2,-1,1,2,3]
random.shuffle(starts)
self.x = starts[0]
self.y = -3
self.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 True
return 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 = 4
if pos[3] >= self.canvas_height:
self.hit_bottom=True
if self.hit_paddle(pos)==True:
self.y=-4
if pos[0] <= 0:
self.x = 4
if pos[2] >= self.canvas_width:
self.x = -4
class Paddle:
def __init__(self,canvas,color):
self.canvas = canvas
self.id = canvas.create_rectangle(0,0,100,10,fill=color)
self.canvas.move(self.id,200,400)
self.x=0
self.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 = 0
elif 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)
效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python使用opencv進(jìn)行人臉識(shí)別
- python實(shí)現(xiàn)讀取并顯示圖片的兩種方法
- Python實(shí)現(xiàn)彈球小游戲的示例代碼
- 用python實(shí)現(xiàn)彈球小游戲
- 用Python寫一個(gè)簡易版彈球游戲
- python 實(shí)現(xiàn)彈球游戲的示例代碼
- Python實(shí)現(xiàn)彈球小游戲
- 使用python和pygame制作擋板彈球游戲
- python pygame實(shí)現(xiàn)擋板彈球游戲
- python運(yùn)用pygame庫實(shí)現(xiàn)雙人彈球小游戲
- python3實(shí)現(xiàn)彈彈球小游戲
- Python基于Tkinter模塊實(shí)現(xiàn)的彈球小游戲
- python編寫彈球游戲的實(shí)現(xiàn)代碼
- Python實(shí)現(xiàn)的彈球小游戲示例
- Python彈球小游戲的項(xiàng)目代碼
相關(guān)文章
20行Python代碼實(shí)現(xiàn)一款永久免費(fèi)PDF編輯工具的實(shí)現(xiàn)
這篇文章主要介紹了20行Python代碼實(shí)現(xiàn)一款永久免費(fèi)PDF編輯工具的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
tensorflow2 自定義損失函數(shù)使用的隱藏坑
本文主要介紹了tensorflow2 自定義損失函數(shù)使用的隱藏坑,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
anaconda?部署Jupyter?Notebook服務(wù)器過程詳解
這篇文章主要為大家介紹了anaconda?部署Jupyter?Notebook服務(wù)器過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
python計(jì)算機(jī)視覺實(shí)現(xiàn)全景圖像拼接示例
這篇文章主要為大家介紹了python計(jì)算機(jī)視覺實(shí)現(xiàn)全景圖像拼接示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
TensorFlow Session會(huì)話控制&Variable變量詳解
今天小編就為大家分享一篇TensorFlow Session會(huì)話控制&Variable變量詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07

