汇智学堂-python小游戏(弹球游戏之九-不同角度的反弹及游戏结束的判断)

3.9不同角度的反弹及游戏结束的判断
现在我们要解决不同角度下,球的反弹及游戏结束的判断。下面是我们要做的事情。
1、解决不同角度下,球的反弹。
2、解决三种情况的游戏结束。第一种球落到了底部。第二种球板撞到了墙的左侧,第三种球板撞到了墙的右侧。

球板中draw实现代码如下:

def draw(self):
global closeornot
pos = self.canvas.coords(self.id)
if pos[0] <= 0:
self.x = 0
closeornot=0
if pos[0]>=700:
self.x=0
closeornot=0
self.canvas.move(self.id,self.x,0)
if self.collision(pos)==True:
self.ball.y=-5
def collision(self,pos):
posball = self.canvas.coords(self.ball.id)
if posball[1]<=pos[3] and posball[2]>=pos[0] and posball[0]<=pos[2]
and posball[3]>=pos[1]:
return True
return False

球中的draw函数实现代码如下:

def draw(self):
global direction,closeornot
position=canvas.coords(self.id)
if position[3]>=800:
closeornot=0
if position[1]<=0:
self.y=5
if position[0]<=0:
self.x=3
if position[2]>=800:
self.x=-3
self.canvas.move(self.id,self.x,self.y)

整合后,完整的代码如下:

#-- coding:GBK --

from tkinter import *
import time
import random
import tkinter.messagebox #messagebox

closeornot=1
position=[1,1,1,1]

tk = Tk()
tk.title(“雷雷的弹球游戏”)
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=800, height=800, bd=0, highlightthickness=0)
canvas.pack()
tk.update()

class racket:
def init(self,canvas,color,ball):
self.canvas=canvas
self.x=0
self.id=canvas.create_rectangle(400, 600, 500, 620, fill =color)
self.canvas.bind_all(’’, self.turn_left)
self.canvas.bind_all(’’, self.turn_right)
self.ball=ball

扫描二维码关注公众号,回复: 5498362 查看本文章
def turn_left(self,evt):
    self.x=-5        
def turn_right(self, evt):
    self.x = 5     
def draw(self):  
    global closeornot
    pos = self.canvas.coords(self.id)
    if pos[0] <= 0:            
        self.x = 0
        closeornot=0           
    if pos[0]>=700:
        self.x=0
        closeornot=0            
    self.canvas.move(self.id,self.x,0)        
    if self.collision(pos)==True:
        self.ball.y=-5       
def collision(self,pos):
    posball = self.canvas.coords(self.ball.id) 
    if posball[1]<=pos[3] and posball[2]>=pos[0] and posball[0]<=pos[2]  \

and posball[3]>=pos[1]:
return True
return False
class ball:
def init(self,canvas,color):

    self.canvas=canvas
    self.a=random.randint(50,600)
    self.b=random.randint(50,200)
    self.id=canvas.create_oval(self.a, self.b, self.a+20, self.b+20, fill =color)
    starts = [-3, -2, -1, 1, 2, 3]
    random.shuffle(starts)
    self.x = starts[0]
    self.y=5
    
def draw(self):        
    global direction,closeornot
    position=canvas.coords(self.id)    
    if position[3]>=800:
        closeornot=0            
    if position[1]<=0:
        self.y=5 
    if position[0]<=0:
        self.x=3            
    if position[2]>=800:
        self.x=-3           
    self.canvas.move(self.id,self.x,self.y)

ball=ball(canvas,‘red’)
racket=racket(canvas,‘green’,ball)

while 1:
if closeornot==1:
racket.draw()
ball.draw()
else:
canvas.create_text(400,420,text=“游戏结束”,fill=‘red’,font=(‘Times’,110))
tk.update_idletasks()
tk.update()
time.sleep(0.05)

运行这段代码,球板撞到左侧墙壁,提示:游戏已结束,见图3-25。球板撞到右侧墙壁,提示:游戏已结束,见图3-26。球撞到底侧墙壁,提示:游戏已结束,见图3-27。

在这里插入图片描述

图3-25
在这里插入图片描述
图3-26
在这里插入图片描述
图3-27

猜你喜欢

转载自blog.csdn.net/weixin_39593940/article/details/88361926