写了一个由小球和球拍构成的小游戏,小球在画布内随机运动,碰到非底部边框与球拍进行反弹,碰到底部game over!
win10 64bit python 3.6.0版本
from tkinter import * #调用thinter这个函数
import random
import time
引用绘图,随机,时间三种函数
tk = Tk() #直接调用thinter中的Tk()
tk.title("Game") #标题为GAME
tk.resizable(0,0) #窗口大小水平与垂直方向上都不可更改
tk.wm_attributes("-topmost",1) #窗口置于其它窗口前面
canvas = Canvas(tk,width = 500,height = 400,bd = 0,highlightthickness=0) #bd=0,highlightthickness=0 让画布更加美观,之外无边框
canvas.pack() #让画布按照前一行给出的宽度与高度调整自身大小
tk.update() #游戏动画初始
运行代码会看到一块500*400的画布
class Ball:
def __init__(self,canvas,paddle,color,hit_bottom): #初始化函数,包括画布与颜色
self.canvas = canvas
self.paddle = paddle
self.id = canvas.create_oval(10,10,25,25,fill=color) #产生的球的编号
self.canvas.move(self.id,225,100) #将它移动到画布的中心
starts = [-3,-2,-1,1,2,3] #选择运行方向
random.shuffle(starts) #将运行方向随机化
self.x = starts[0] #初始X方向
self.y = -3 #初始y方向
self.canvas_height = self.canvas.winfo_height() #得到小球当前的高度位置
self.canvas_width = self.canvas.winfo_width() #得到小球当前的宽度位置
self.hit_bottom = False #初始触底为假
def hit_paddle(self,pos): #判断ball是否撞板
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[1] <= paddle_pos[3]:
return True
return False
def draw(self): #让ball动起来
self.canvas.move(self.id,self.x,self.y)
pos = self.canvas.coords(self.id)
if pos[1] <= 0:
self.y = 3
if pos[3] >= self.canvas_height:
self.y = -3
self.hit_bottom = True
if self.hit_paddle(pos) == True: #撞板反弹
self.y = -3
if pos[0] <= 0:
self.x = 3
if pos[2] >= self.canvas_width:
self.x = -3
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,300)
self.x=0
self.canvas_width = self.canvas.winfo_width()
self.canvas.bind_all('' ,self.turn_left) #按键判定
self.canvas.bind_all('' ,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
elif pos[2] >= self.canvas_width:
self.x = 0
def turn_left(self,evt): #左右限位
self.x = -2
def turn_right(self,evt):
self.x = 2
paddle= Paddle(canvas,'blue') #蓝色的球拍
ball = Ball(canvas,paddle,'red',True) #红色的小球
while True:
if ball.hit_bottom == False:
ball.draw()
paddle.draw()
tk.update_idletasks()
tk.update() #更新画面
time.sleep(0.01) #调整更新频率
from tkinter import * #调用thinter这个函数
import random
import time
class Ball:
def __init__(self,canvas,paddle,color,hit_bottom): #初始化函数,包括画布与颜色
self.canvas = canvas
self.paddle = paddle
self.id = canvas.create_oval(10,10,25,25,fill=color) #产生的球的编号
self.canvas.move(self.id,225,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[1] <= 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 = 3
if pos[3] >= self.canvas_height:
self.y = -3
self.hit_bottom = True
if self.hit_paddle(pos) == True:
self.y = -3
if pos[0] <= 0:
self.x = 3
if pos[2] >= self.canvas_width:
self.x = -3
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,300)
self.x=0
self.canvas_width = self.canvas.winfo_width()
self.canvas.bind_all('' ,self.turn_left)
self.canvas.bind_all('' ,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
elif pos[2] >= self.canvas_width:
self.x = 0
def turn_left(self,evt):
self.x = -2
def turn_right(self,evt):
self.x = 2
tk = Tk() #直接调用thinter中的Tk()
tk.title("Game") #标题为GAME
tk.resizable(0,0) #窗口大小水平与垂直方向上都不可更改
tk.wm_attributes("-topmost",1) #窗口置于其它窗口前面
canvas = Canvas(tk,width = 500,height = 400,bd = 0,highlightthickness=0) #bd=0,highlightthickness=0 让画布更加美观,之外无边框
canvas.pack() #让画布按照前一行给出的宽度与高度调整自身大小
tk.update() #游戏动画初始
paddle= Paddle(canvas,'blue')
ball = Ball(canvas,paddle,'red',True)
while True:
if ball.hit_bottom == False:
ball.draw()
paddle.draw()
tk.update_idletasks()
tk.update()
time.sleep(0.01)
7.1球拍启动就是按着固定方向运行,因为它在while里,这里可以改进,让它和我们键盘的节奏一样,欢迎大家修改
7.2到达左右限位,继续点这个方向运行,球拍会慢慢消失
7.3没有计分系统,没有重启游戏功能,可以增加这个功能用来增强趣味性
7.4色彩,背景可以优化