说明:在游戏界面点击鼠标左键开始游戏,小球的移动速度每次游戏均为随机获得,键盘左右键控制木板的移动,每接到一次小球score+2,小球落地游戏结束
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,outline=color)
self.canvas.move(self.id,245,100)
#游戏随机获得小球的移动速度
starts=[-3,-2,-1,1,2,3]
random.shuffle(starts)
self.x=starts[0]
self.y=starts[5]
self.canvas_height=self.canvas.winfo_height() #获取画布当前的高度
self.canvas_width=self.canvas.winfo_width() #获取画布当前的宽度
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
def draw(self,display):
self.display=display
self.canvas.move(self.id,self.x,self.y)
pos=self.canvas.coords(self.id) #获取当前小球左上角坐标(x1,y1)和右下角坐标(x2,y2)
#判断小球是否超出画布边界
if self.hit_paddle(pos)==True:
self.display.update(1,-1) #小球与木板碰撞增加分数
self.y=-self.y
if pos[0]<=0:
self.x=-self.x
if pos[1]<=0:
self.y=-self.y
if pos[2]>=self.canvas_width:
self.x=-self.x
if pos[3]>=self.canvas_height:
# self.y=-self.y
self.hit_bottom=True
self.display.update(0, 2) #小球落地更新游戏状态
def start(self,event,display):
self.display=display
global isStart
if isStart==False:
self.display.update(0, 1) #游戏开始更新游戏状态
self.hit_bottom = False
isStart = True
#创建木板
class Paddle:
def __init__(self,canvas,color):
self.canvas=canvas
self.id=canvas.create_rectangle(0,0,100,10,fill=color,outline=color)
self.canvas.move(self.id,200,350)
self.x=0
self.canvas_width=self.canvas.winfo_width()
#创建键盘监听事件控制木板左右移动
self.canvas.bind_all('<KeyPress-Left>',self.turn_left)
self.canvas.bind_all('<KeyPress-Right>',self.turn_right)
def draw(self):
self.canvas.move(self.id,self.x,0)
pos=self.canvas.coords(self.id) #获取木板坐标
#控制木板使其运动不能超出画布
if pos[0]<=0:
self.x=0
if pos[2]>=self.canvas_width:
self.x=0
def turn_left(self,event):
self.x=-2
pos = self.canvas.coords(self.id)
if pos[0] <= 0:
self.x = 0
def turn_right(self,event):
self.x=2
pos = self.canvas.coords(self.id)
if pos[2]>=self.canvas_width:
self.x=0
#更新游戏得分和游戏状态
class Display:
score=0
def __init__(self,canvas):
self.canvas=canvas
self.text1_id = canvas.create_text(10, 370, text="Score:0",anchor=NW,font=('Courier',15))
self.text2_id=canvas.create_text(430,380,text="Click here",font=('Courier',15))
def update(self,isScore,State):
if isScore:
self.score=self.score+2
self.canvas.itemconfig(self.text1_id,text="Score:"+str(self.score))
if State==1:
self.canvas.itemconfig(self.text2_id, text="Running")
elif State==2:
self.canvas.itemconfig(self.text2_id, text="GameOver")
def getScore(self):
return self.score
tk=Tk()
tk.title("A_Funny_Game") #为窗口自定义一个标题
tk.resizable(0,0) #窗口的大小在水平方向和竖直方向上都不可调整
tk.wm_attributes("-topmost",1) #将当前画布的窗口置于其他窗口之上
canvas=Canvas(tk,width=500,height=400,bd=0,highlightthickness=0) #最后两个参数确保画布之外没有边框
canvas.pack()
tk.update()
paddle=Paddle(canvas,"green") #创建木板
ball=Ball(canvas,paddle,"red") #创建小球
display=Display(canvas) #创建显示文本
isStart=False
#设置游戏开始条件,即在游戏界面点击鼠标左键
if isStart==False:
canvas.bind_all('<Button-1>', lambda x:ball.start(x,display))
while True:
if ball.hit_bottom == False:
ball.draw(display)
paddle.draw()
tk.update_idletasks() #与update()共同实现强制tkinter快速重画屏幕
tk.update()
time.sleep(0.01)