为乒乓球游戏添加障碍物生成和检测

时间:2018-04-03 16:48:28

标签: python python-3.x

这个项目没有pygame或其他没有内置到Python 3中的库。我已经制作了一个乒乓球游戏,一个球拍和一个球在屏幕上移动。当球击中球拍时,它会像往常一样在任何类似的比赛中反弹。我想用球拍和球在框架的顶部中间部分生成一个矩形形状的网格,并且当球击中一个矩形时,矩形消失。粗略地说,有什么方法可以做到这一点以及它会是什么样子?以下是我目前正在使用的内容:

from tkinter import *
import tkinter.font
import time

class Display(Frame):

    def __init__(self):
        Frame.__init__(self)
        self.master.title("Animation")
        self.grid()
        horizontal_direction = "east"
        vertical_direction = "south"
        self.canvas_width = 800
        self.canvas_height = 400
        self.paddle_x = 20
        self.paddle_y = 80
        self.left_rect_side = 360 #forposition
        self.canvas = Canvas(self, width=self.canvas_width, height=self.canvas_height, bg = "white")
        self.canvas.grid(row = 1, column = 0)
        self.master.bind('<Left>', lambda event: self.leftKey(self))
        self.master.bind('<Right>', lambda event: self.rightKey(self))
        self.x = 5
        self.y = 5
        diameter = 20
        self.canvas.create_oval(self.x, self.y, self.x + diameter, self.y + diameter, outline="#000000"
                           , fill="red", tags="circle")
        self.canvas.create_rectangle(self.canvas_width/2 - self.paddle_y/2, self.canvas_height - self.paddle_x,
                                     self.canvas_width/2 + self.paddle_y/2, self.canvas_height,
                                     fill="black", tags="paddle")

        fontx = tkinter.font.Font(family = "Verdana", size = 20)
        self.lives = 5
        self.lifedisplay = Label(self, width = -800, height = -20,
                                 font = fontx, text = "Lives left: " + str(self.lives))
        self.lifedisplay.grid(row = 0, column = 0)
        mvt = 2

        while True:

            if self.y + diameter > self.canvas_height:
                self.lives -= 1
                self.lifedisplay.configure(text = "Lives left: " + str(self.lives))

            if self.lives <= 0:
                self.canvas.move("circle", -self.x, -self.y)
                break

            if self.y + diameter >= self.canvas_height - self.paddle_x:
                if self.x + diameter > self.left_rect_side and self.x < self.left_rect_side + self.paddle_y:
                    vertical_direction = "north"

            if horizontal_direction == "east":
                if self.x + diameter >= self.canvas_width:
                    horizontal_direction = "west"
                else:
                    self.canvas.move("circle", mvt, 0)
                    self.x += mvt
            else:
                if self.x + diameter <= diameter:
                    horizontal_direction = "east"
                else:
                    self.canvas.move("circle", -mvt, 0)
                    self.x -= mvt

            if vertical_direction == "south":
                if self.y + diameter >= self.canvas_height:
                    vertical_direction = "north"
                    self.canvas.move("circle", 0, -mvt)
                    self.y -= mvt
                else:
                    self.canvas.move("circle", 0, mvt)
                    self.y += mvt
            else:
                if self.y + diameter <= diameter:
                    vertical_direction = "south"
                else:
                    self.canvas.move("circle", 0, -mvt)
                    self.y -= mvt

            self.canvas.update()
            self.canvas.after(15)

    @staticmethod
    def leftKey(self):
        if self.left_rect_side >= 10:
            self.canvas.move("paddle", -5, 0)
            self.left_rect_side -= 5
            self.canvas.update()

    @staticmethod
    def rightKey(self):
        if self.left_rect_side <= self.canvas_width - self.paddle_y - 5:
            self.canvas.move("paddle", 5, 0)
            self.left_rect_side += 5
            self.canvas.update()

def main():
    Display().mainloop()

main()

1 个答案:

答案 0 :(得分:1)

为此,您应该列出一堆具有4个属性的矩形对象:

  • x坐标
  • y坐标
  • 宽度
  • 高度

此外,您必须计算出圆心的x / y坐标。然后,每个帧都有一个检查圆和每个矩形之间碰撞的方法。如果矩形被圆圈击中,则从矩形列表中删除矩形,然后仅将列表中的矩形绘制到屏幕上。

检查碰撞是最困难的部分。我建议查看这个StackOverflow答案:

Circle-Rectangle collision detection (intersection)

下一篇文章可能有点难以理解,但也要检查一下:

https://yal.cc/rectangle-circle-intersection-test/

我不打算为你编写代码(这不是SO的用途),但我希望这有帮助。如果您需要帮助理解,请告诉我。