# -*- coding = utf-8 -*-
# @Time : 2021/11/17 9:15
# @Author : luxiaoguo
# @File : 弹球游戏.py
# @Software : PyCharm
import random
import time
from tkinter import *
# 生成小球类
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]
#shuffle()方法将序列的所有元素随机排序
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 draw(self):
self.canvas.move(self.id,self.x,self.y)
pos = self.canvas.coords(self.id) # coord = 坐标,返回小球框架坐标,左上角x1,y1 右下角x2,y2
if pos[1] <= 0:
self.y = 3
if pos[3] >= self.canvas_height: # 碰到了屏幕底部,即失败
self.y = -3
if self.hit_paddle(pos) == True: # 小球和球拍相撞
self.y = -3
label0["text"] = "score:" + str(score.score())
if score.x % 100 == 0:
paddle.length /= 2
canvas.coords(paddle.id, (0, 0, paddle.length, 10))
paddle.canvas.move(paddle.id, 200, 300)
if pos[3] >= self.canvas_height:
self.hit_bottom = True
if pos[0] <= 0:
self.x = 3
if pos[2] >=self.canvas_width:
self.x = -3
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,length,color):
self.length = length
self.canvas = canvas
self.id = canvas.create_rectangle(0,0,length,10,fill=color)
self.canvas.move(self.id,200,300)
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 turn_left(self,evt):
self.x = -2
def turn_right(self,evt):
self.x = 2
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
# 记分类
class scoreboard:
def __init__(self):
self.x = 0
def score(self):
self.x += 10
return self.x
# 生命类
class die:
def __init__(self):
self.life = 2 # 小球生命次数
def balldie(self): # 死亡一次,生命减1
if self.life >= 0:
self.life -= 1
# 游戏窗体
tk = Tk()
tk.title("撞球游戏")
tk.resizable(0,0) #窗口是否可变(长、宽),也可用0,1表示
tk.wm_attributes("-topmost",1) # 窗口永远在前
canvas = Canvas(tk,width=500,height=400,bd=0,highlightthickness=0) # 画布之外没有边框
canvas.pack()
label0 = Label(tk,text = 'score:0')# 计分板
label0.pack()# 这句不加,显示不出label来
tk.update() # 刷新窗口
#实例化
paddle = Paddle(canvas,200, 'pink')
ball = Ball(canvas, paddle, 'blue')
score = scoreboard()
balldie=die()
#游戏不断循环
while 1:
if ball.hit_bottom == False:
ball.draw()
paddle.draw()
label0["text"]="score:"+str(score.x)+",life:"+str(balldie.life)
else:# 假设触碰到了底部,小球生命减1
if balldie.life>0:
balldie.balldie()
del ball
ball = Ball(canvas, paddle, 'red')
label0["text"]=label0["text"]+",life:"+str(balldie.life)
else:
label0["text"]="score:"+str(score.x)+",life:"+str(balldie.life)+",game over"
tk.update_idletasks()
tk.update()
time.sleep(0.01)
python弹球游戏
最新推荐文章于 2024-08-13 21:45:04 发布