使用 Python 捕捉球游戏


Python 还可以用来创建电脑游戏。在这篇文章中,我们将了解如何使用 Python 创建球捕捉游戏。在这个游戏中,一个球不断地从画布窗口的顶部掉落,窗口底部有一个横条。提供了两个按钮用于左右移动横条。使用鼠标按钮按下,我们移动底部的横条来捕捉下落的球。在不同的时间,球从不同的位置掉落。

方法

构建游戏的步骤如下所述。

  • 步骤 1 − 绘制一个 tkinter 矩形画布,可用于容纳其他布局,如各种图形、文本和图像等。

  • 步骤 2 − 创建将从顶部掉落的球。为此使用 create_oval 方法。它需要 4 个坐标来创建一个椭圆,它是圆形和矩形的混合。

  • 步骤 3 − 在底部创建横条,该横条将在鼠标按钮按下时从左到右移动。可以使用 create_rectangle 方法来实现。

  • 步骤 4 − 使用 canvas.move 方法移动球或横条。此方法用于与其关联的对象的垂直和水平移动。

  • 步骤 5 − 创建将用于移动底部横条的按钮。将应用事件,这些事件将在单击按钮时触发。

程序

以下是基于上述步骤使用相关方法和对象的完整程序。

示例

#Catching the ball game using Python

from tkinter import Tk, Button, Label
from tkinter import Canvas
from random import randint
base = Tk()
base.title("BALL GAME")
base.resizable(False, False)
color = Canvas(base, width=590, height=610)
color.pack()
standard = 0
length = 5
marks = 0
class model:
   def __init__(self, color, m1, n1, m2, n2):
      self.m1 = m1
      self.n1 = n1
      self.m2 = m2
      self.n2 = n2
      self.color = color
      self.circle = color.create_oval(self.m1, self.n1, self.m2, self.n2,fill="blue", tags='dot1')
   def Game(self):
      offset = 5
      global standard
      if standard >= 510:
         global length, marks, next
         if (length - offset <= self.m1 and
            length + 40 + offset >= self.m2):
            marks += 5
            color.delete('dot1')
            game_play()
         else:
            color.delete('dot1')
            slide.remove(self)
            result()
         return

      standard += 1
      self.color.move(self.circle, 0, 1)
      self.color.after(10, self.Game)
class slide:
   def __init__(self, color, m1, n1, m2, n2):
      self.m1 = m1
      self.n1 = n1
      self.m2 = m2
      self.n2 = n2
      self.color = color
      self.num = color.create_rectangle(self.m1, self.n1, self.m2, self.n2, fill="green",       tags='dot2')
   def push(self, num):
      global length
      if (num == 1):
         self.color.move(self.num, 20, 0)
         length += 20
   else:
      self.color.move(self.num, -20, 0)
      length -= 20
   def remove(self):
      color.delete('dot2')
def game_play():
   global standard
   standard = 0
   size = randint(0, 570)
   game1 = model(color, size, 20, size + 30, 50)
   game1.Game()
def result():
   base2 = Tk()
   base2.title("THE BALL GAME")
   base2.resizable(False, False)
   set = Canvas(base2, width=300, height=300)
   set.pack()
   z = Label(set, text="\nGame over\n\nYou have scored = " + str(marks) + "\n\n")
   z.pack()
   btx = Button(set, text="Enter if you want to play again", bg="yellow", command=lambda:    repeat(base2))
   btx.pack()
   bty = Button(set, text=" CLOSE ", bg="red",command=lambda: destroy(base2))
   bty.pack()
def repeat(base2):
   base2.destroy()
   function()
def destroy(base2):
   base2.destroy()
   base.destroy()
def function():
   global marks, length
   marks = 0
   length = 0
   x1 = slide(color, 5, 560, 45, 575)
   Bt0 = Button(color, text="move right**", bg="pink",command=lambda: x1.push(1))
   Bt0.place(x=335, y=580)
   Bt1 = Button(color, text="**move left ", bg="pink", command=lambda: x1.push(0))
   Bt1.place(x=260, y=580)
   game_play()
   base.mainloop()
if (__name__ == "__main__"):
   function()

输出

运行以上代码,我们将得到以下结果:

更新于: 2020年7月10日

1K+ 浏览量

开启您的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.