Python實(shí)現(xiàn)的彈球小游戲示例
本文實(shí)例講述了Python實(shí)現(xiàn)的彈球小游戲。分享給大家供大家參考,具體如下:
彈球
1. Ball 類
draw負(fù)責(zé)移動Ball
碰撞檢測,反彈,Ball檢測Paddle
2.Paddle類
draw負(fù)責(zé)移動Paddle
碰撞檢測,確定能不能繼續(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)典教程》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
如何將Yolov5的detect.py修改為可以直接調(diào)用的函數(shù)詳解
YOLOv4還沒有退熱,YOLOv5已經(jīng)發(fā)布,下面這篇文章主要給大家介紹了關(guān)于如何將Yolov5的detect.py修改為可以直接調(diào)用的函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
使用python驗(yàn)證代理ip是否可用的實(shí)現(xiàn)方法
驗(yàn)證代理IP是否可用。原理是使用代理IP訪問指定網(wǎng)站,如果返回狀態(tài)為200,表示這個(gè)代理是可以使用的。這篇文章重點(diǎn)給大家介紹使用python驗(yàn)證代理ip是否可用的實(shí)現(xiàn)方法,感興趣的朋友一起看看吧2018-07-07
python多進(jìn)程控制學(xué)習(xí)小結(jié)
這篇文章主要介紹了python多進(jìn)程控制學(xué)習(xí)小結(jié),想要充分利用多核CPU資源,Python中大部分情況下都需要使用多進(jìn)程,Python中提供了multiprocessing這個(gè)包實(shí)現(xiàn)多進(jìn)程。感興趣的小伙伴們可以參考一下2018-10-10
python調(diào)用百度API實(shí)現(xiàn)人臉識別
這篇文章主要介紹了python調(diào)用百度API實(shí)現(xiàn)人臉識別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

