在本次课程中,我们使用 Python 创建了经典的 "抓蛋 "游戏。在这个游戏中,每抓到一个鸡蛋就能赢得 10 分,而每掉落一个鸡蛋就会损失一条命。
小时候,我们都玩过 "抓鸡蛋 "游戏。我们使用海龟软件包在 Python 中实现了这个游戏。在这个抓蛋游戏中,您必须用一个篮子抓住掉落在屏幕上的鸡蛋,这将考验您的注意力和反应能力。要设计一个鸡蛋捕手游戏,您需要创建一个动画,显示鸡蛋随机掉落的位置和一个篮子将鸡蛋全部接住的过程。
构建地板、篮子和鸡蛋是用 Python 创建鸡蛋捕手游戏的第一步。游戏开始时,鸡蛋会在地板上慢慢滑动,产生一个鸡蛋掉落的动画信号。然后,我们可以使用循环来跟踪鸡蛋是还在篮子里还是已经掉到了地上。当鸡蛋被接住或掉落时,事件结束。因此,彩蛋将被收回,游戏将调整分数,如果彩蛋被抓到篮子里,分数将提高;如果彩蛋掉到地上,分数将降低。
制作这款游戏将使用三个 Python 模块:
- 使用
itertools
来改变掉落鸡蛋的颜色。 - 要让彩蛋随机出现,请使用
random
。 - 要在屏幕上制作游戏动画,请使用
Tkinter
。
概要:
要运行这段代码,我们需要导入三个模块。我们可以简单地创建这个游戏,但我们必须了解一下这些模块在这段代码中的用法。
项目先决条件:
tkinter:
tkinter 是 Python 中的一个模块,是 Python 编程中最基本的图形用户界面框架。
Itertools:
它是 Python 中的一个模块,用于遍历可以使用 for 循环遍历的数据结构。这类数据结构也被称为可迭代数据。该模块是一种快速、内存效率高的工具,既可单独使用,也可组合使用,形成迭代代数。
random:
它是 Python 中的一个模块,randint(),返回给定范围内的一个随机数;choice(),从给定序列中返回一个随机元素。
代码执行:
这样,我们就必须导入这些模块。
from itertools import cycle
from random import randrange
from tkinter import Canvas, Tk, messagebox, font
我们必须为矩形形状设置高度和宽度,我们可以借助 Tkinter 中的 canvas 方法创建这个矩形。
这样,我们就必须设置鸡蛋的高度和重量。
canvas_width = 800
canvas_height = 400
root = Tk()
root.title("Egg Catcher")
c = Canvas(root, width=canvas_width, height=canvas_height, background="deep sky blue")
c.create_rectangle(-5, canvas_height-100, canvas_width+5, canvas_height+5, fill="sea green", width=0)
c.create_oval(-80, -80, 120, 120, fill='orange', width=0)
c.pack()
color_cycle = cycle(["light blue", "light green", "light pink", "light yellow", "light cyan"])
egg_width = 45
egg_height = 55
egg_score = 10
egg_speed = 500
egg_interval = 4000
difficulty = 0.95
catcher_color = "blue"
catcher_width = 100
catcher_height = 100
catcher_startx = canvas_width / 2 - catcher_width / 2
catcher_starty = canvas_height - catcher_height - 20
catcher_startx2 = catcher_startx + catcher_width
catcher_starty2 = catcher_starty + catcher_height
catcher = c.create_arc(catcher_startx, catcher_starty, catcher_startx2, catcher_starty2, start=200, extent=140, style="arc", outline=catcher_color, width=3)
game_font = font.nametofont("TkFixedFont")
game_font.config(size=18)
score = 0
score_text = c.create_text(10, 10, anchor="nw", font=game_font, fill="darkblue", text="Score: "+ str(score))
lives_remaining = 3
lives_text = c.create_text(canvas_width-10, 10, anchor="ne", font=game_font, fill="darkblue", text="Lives: "+ str(lives_remaining))
eggs = []
在这里,我们定义了八种不同的功能。
-
create_egg():我们使用该函数创建鸡蛋。
-
move_eggs():使用该函数移动鸡蛋。
-
egg_dropped(egg):该函数用于移除和删除彩蛋。
-
lose_a_life():该函数会告诉我们游戏还剩下多少条命。
-
check_catch():此函数用于检查捕获的数量。
-
increase_score(points):该函数用于显示我们的得分。
-
move_left(event):该函数用于将篮筐移动到左侧。
-
move_right(event):该函数用于将篮筐向左侧移动。
def create_egg():
x = randrange(10, 740)
y = 40
new_egg = c.create_oval(x, y, x+egg_width, y+egg_height, fill=next(color_cycle), width=0)
eggs.append(new_egg)
root.after(egg_interval, create_egg)
def move_eggs():
for egg in eggs:
(eggx, eggy, eggx2, eggy2) = c.coords(egg)
c.move(egg, 0, 10)
if eggy2 > canvas_height:
egg_dropped(egg)
root.after(egg_speed, move_eggs)
def egg_dropped(egg):
eggs.remove(egg)
c.delete(egg)
lose_a_life()
if lives_remaining == 0:
messagebox.showinfo("Game Over!", "Final Score: "+ str(score))
root.destroy()
def lose_a_life():
global lives_remaining
lives_remaining -= 1
c.itemconfigure(lives_text, text="Lives: "+ str(lives_remaining))
def check_catch():
(catcherx, catchery, catcherx2, catchery2) = c.coords(catcher)
for egg in eggs:
(eggx, eggy, eggx2, eggy2) = c.coords(egg)
if catcherx < eggx and eggx2 < catcherx2 and catchery2 - eggy2 < 40:
eggs.remove(egg)
c.delete(egg)
increase_score(egg_score)
root.after(100, check_catch)
def increase_score(points):
global score, egg_speed, egg_interval
score += points
egg_speed = int(egg_speed * difficulty)
egg_interval = int(egg_interval * difficulty)
c.itemconfigure(score_text, text="Score: "+ str(score))
def move_left(event):
(x1, y1, x2, y2) = c.coords(catcher)
if x1 > 0:
c.move(catcher, -20, 0)
def move_right(event):
(x1, y1, x2, y2) = c.coords(catcher)
if x2 < canvas_width:
c.move(catcher, 20, 0)
c.bind("<Left>", move_left)
c.bind("<Right>", move_right)
c.focus_set()
root.after(1000, create_egg)
root.after(1000, move_eggs)
root.after(1000, check_catch)
root.mainloop()
源代码:
以下是该项目的完整源代码。
from itertools import cycle
from random import randrange
from tkinter import Canvas, Tk, messagebox, font
canvas_width = 800
canvas_height = 400
root = Tk()
root.title("Egg Catcher")
c = Canvas(root, width=canvas_width, height=canvas_height, background="deep sky blue")
c.create_rectangle(-5, canvas_height-100, canvas_width+5, canvas_height+5, fill="sea green", width=0)
c.create_oval(-80, -80, 120, 120, fill='orange', width=0)
c.pack()
color_cycle = cycle(["light blue", "light green", "light pink", "light yellow", "light cyan"])
egg_width = 45
egg_height = 55
egg_score = 10
egg_speed = 500
egg_interval = 4000
difficulty = 0.95
catcher_color = "blue"
catcher_width = 100
catcher_height = 100
catcher_startx = canvas_width / 2 - catcher_width / 2
catcher_starty = canvas_height - catcher_height - 20
catcher_startx2 = catcher_startx + catcher_width
catcher_starty2 = catcher_starty + catcher_height
catcher = c.create_arc(catcher_startx, catcher_starty, catcher_startx2, catcher_starty2, start=200, extent=140, style="arc", outline=catcher_color, width=3)
game_font = font.nametofont("TkFixedFont")
game_font.config(size=18)
score = 0
score_text = c.create_text(10, 10, anchor="nw", font=game_font, fill="darkblue", text="Score: "+ str(score))
lives_remaining = 3
lives_text = c.create_text(canvas_width-10, 10, anchor="ne", font=game_font, fill="darkblue", text="Lives: "+ str(lives_remaining))
eggs = []
def create_egg():
x = randrange(10, 740)
y = 40
new_egg = c.create_oval(x, y, x+egg_width, y+egg_height, fill=next(color_cycle), width=0)
eggs.append(new_egg)
root.after(egg_interval, create_egg)
def move_eggs():
for egg in eggs:
(eggx, eggy, eggx2, eggy2) = c.coords(egg)
c.move(egg, 0, 10)
if eggy2 > canvas_height:
egg_dropped(egg)
root.after(egg_speed, move_eggs)
def egg_dropped(egg):
eggs.remove(egg)
c.delete(egg)
lose_a_life()
if lives_remaining == 0:
messagebox.showinfo("Game Over!", "Final Score: "+ str(score))
root.destroy()
def lose_a_life():
global lives_remaining
lives_remaining -= 1
c.itemconfigure(lives_text, text="Lives: "+ str(lives_remaining))
def check_catch():
(catcherx, catchery, catcherx2, catchery2) = c.coords(catcher)
for egg in eggs:
(eggx, eggy, eggx2, eggy2) = c.coords(egg)
if catcherx < eggx and eggx2 < catcherx2 and catchery2 - eggy2 < 40:
eggs.remove(egg)
c.delete(egg)
increase_score(egg_score)
root.after(100, check_catch)
def increase_score(points):
global score, egg_speed, egg_interval
score += points
egg_speed = int(egg_speed * difficulty)
egg_interval = int(egg_interval * difficulty)
c.itemconfigure(score_text, text="Score: "+ str(score))
def move_left(event):
(x1, y1, x2, y2) = c.coords(catcher)
if x1 > 0:
c.move(catcher, -20, 0)
def move_right(event):
(x1, y1, x2, y2) = c.coords(catcher)
if x2 < canvas_width:
c.move(catcher, 20, 0)
c.bind("<Left>", move_left)
c.bind("<Right>", move_right)
c.focus_set()
root.after(1000, create_egg)
root.after(1000, move_eggs)
root.after(1000, check_catch)
root.mainloop()
运行上述命令后,你将打开一个新窗口,并在该窗口中启动 "彩蛋捕手 "游戏。
这是代码的输出结果。我们可以看到有一个鸡蛋朝地面飞来,而地面上有一个蓝色的 U 形篮子。现在,我们必须使用左箭头键和右箭头键朝左右移动,将鸡蛋抓进篮子里。
这是我的最终得分,我得了 130 分。
我们成功地用 python 制作了一个抓蛋游戏。