python實(shí)現(xiàn)簡(jiǎn)單反彈球游戲
python簡(jiǎn)單游戲-反彈球,供大家參考,具體內(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)
效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python使用opencv進(jìn)行人臉識(shí)別
- python實(shí)現(xiàn)讀取并顯示圖片的兩種方法
- Python實(shí)現(xiàn)彈球小游戲的示例代碼
- 用python實(shí)現(xiàn)彈球小游戲
- 用Python寫一個(gè)簡(jiǎn)易版彈球游戲
- 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)文章
python爬蟲之利用selenium+opencv識(shí)別滑動(dòng)驗(yàn)證并模擬登陸知乎功能
本文給大家介紹python爬蟲之利用selenium+opencv識(shí)別滑動(dòng)驗(yàn)證并模擬登陸知乎功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-09-09
一篇文章教你用python畫動(dòng)態(tài)愛心表白
這篇文章主要給大家介紹了關(guān)于如何用python畫動(dòng)態(tài)愛心表白的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
淺談pytorch中的nn.Sequential(*net[3: 5])是啥意思
這篇文章主要介紹了pytorch中的nn.Sequential(*net[3: 5])是啥意思,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
anaconda jupyter不能導(dǎo)入安裝的lightgbm解決方案
這篇文章主要介紹了anaconda jupyter不能導(dǎo)入安裝的lightgbm解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
利用python3 的pygame模塊實(shí)現(xiàn)塔防游戲
這篇文章主要介紹了利用python3 的pygame模塊實(shí)現(xiàn)塔防游戲,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
Python pandas的describe函數(shù)參數(shù)示例詳解
describe()函數(shù)是pandas 中一個(gè)十分實(shí)用的工具,用于快速獲取數(shù)據(jù)集的描述性統(tǒng)計(jì)信息,本文詳細(xì)介紹了該函數(shù)的各種參數(shù)及其用法,包括控制輸出的百分位數(shù)、列類型以及是否將日期時(shí)間列視為數(shù)值型列等,感興趣的朋友一起看看吧2018-04-04
Python如何使用Eel和HTML開發(fā)桌面應(yīng)用
這篇文章主要介紹了Python使用Eel和HTML開發(fā)桌面應(yīng)用問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
只需要100行Python代碼就可以實(shí)現(xiàn)的貪吃蛇小游戲
貪吃蛇小游戲相信80、90后小時(shí)候肯定都玩過,那么你知道如果通過Python來實(shí)現(xiàn)嗎?今天就來教大家,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
python str字符串轉(zhuǎn)uuid實(shí)例
這篇文章主要介紹了python str字符串轉(zhuǎn)uuid實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03

