最近,我做了一个项目,基本上是一只飞扬的小鸟(有点),你可以使用箭头键上下左右移动来躲避障碍物,我编写了用于创建和移动障碍物的代码。但它不起作用。
我尝试搜索网络和所有内容,但仍然无法解决它。要了解我的期望,请参阅我从头开始制作的这个项目 - Dodging Game
from itertools import cycle
from random import randrange
from tkinter import Canvas, Tk, messagebox, font
canvas_width = 1400
canvas_height = 700
root = Tk()
root.title("Basic Jumper Game")
c = Canvas(root, width=canvas_width, height=canvas_height, background="sky blue")
c.create_rectangle(-5, canvas_height - 100, canvas_width + 5, canvas_height + 5, fill="forest green", width=0)
c.create_oval(-80, -80, 120, 120, fill='orange', width=0)
c.pack()
color_cycle = cycle(["red2", "yellow", "lime green", "deep sky blue"])
spike_score = 10
spike_speed = 5
spike_interval = 4000
jumper = c.create_rectangle(50, 547, 100, 597, fill='cyan', outline='dark turquoise', width=8)
game_font = font.nametofont("TkFixedFont")
game_font.config(size=30)
score = 0
score_text = c.create_text(575, 10, anchor="nw", font=game_font, fill="black", text="Score: " + str(score))
spikes = []
lives = 1
def create_spike():
y = randrange(0, 100)
new_spike = c.create_rectangle(1300, 600 - y, 1400, 200 - y, fill='red2', outline='red', width=4)
spikes.append(new_spike)
root.after(spike_interval, create_spike)
def move_spikes():
for spike in spikes:
(spikex, spikey, spikex2, spikey2) = c.coords(spike)
c.move(spike, -5, 0)
if spikex2 < 0:
spike_cleared(spike)
root.after(spike_speed, move_spikes)
def spike_cleared(spike):
spikes.remove(spike)
c.delete(spike)
(jumperx, jumpery, jumperx2, jumpery2) = c.coords(jumper)
(spikex, spikey, spikex2, spikey2) = c.coords(spike)
if jumper.colliderect(spike):
lose_a_life()
if lives == 0:
messagebox.showinfo("Game Over!", "Final Score: " + str(score))
root.destroy()
def lose_a_life():
global lives
lives -= 1
def move_up(event):
(x1, y1, x2, y2) = c.coords(jumper)
if y1 > 0:
c.move(jumper, 0, -20)
if y1 < 8:
c.move(jumper, 0, 20)
def move_down(event):
(x1, y1, x2, y2) = c.coords(jumper)
if y1 > 0:
c.move(jumper, 0, 20)
if y2 > 577:
c.move(jumper, 0, -20)
def move_left(event):
(x1, y1, x2, y2) = c.coords(jumper)
if x1 > 0:
c.move(jumper, -20, 0)
def move_right(event):
(x1, y1, x2, y2) = c.coords(jumper)
if x2 < canvas_width:
c.move(jumper, 20, 0)
c.bind("<Up>", move_up)
c.bind("<Down>", move_down)
c.bind("<Left>", move_left)
c.bind("<Right>", move_right)
c.focus_set()
root.after(1000, create_spike)
root.after(1000, move_spikes)
root.mainloop()
该代码似乎没有正确检测尖刺与玩家之间的碰撞。另外,你没有增加分数,而且你尝试检测碰撞的方式有点问题。以下是对代码的更正,可以使其按预期工作:
from itertools import cycle
from random import randrange
from tkinter import Canvas, Tk, messagebox, font
canvas_width = 1400
canvas_height = 700
root = Tk()
root.title("Basic Jumper Game")
c = Canvas(root, width=canvas_width, height=canvas_height, background="sky blue")
c.create_rectangle(-5, canvas_height - 100, canvas_width + 5, canvas_height + 5, fill="forest green", width=0)
c.create_oval(-80, -80, 120, 120, fill='orange', width=0)
c.pack()
color_cycle = cycle(["red2", "yellow", "lime green", "deep sky blue"])
spike_width = 100
spike_score = 10
spike_speed = 5
spike_interval = 4000
jumper = c.create_rectangle(50, 547, 100, 597, fill='cyan', outline='dark turquoise', width=8)
game_font = font.nametofont("TkFixedFont")
game_font.config(size=30)
score = 0
score_text = c.create_text(575, 10, anchor="nw", font=game_font, fill="black", text="Score: " + str(score))
spikes = []
lives = 1
def create_spike():
y = randrange(0, 100)
new_spike = c.create_rectangle(1300, 600 - y, 1400, 200 - y, fill='red2', outline='red', width=4)
spikes.append(new_spike)
root.after(spike_interval, create_spike)
def move_spikes():
global score
for spike in spikes:
(spikex, spikey, spikex2, spikey2) = c.coords(spike)
c.move(spike, -spike_speed, 0)
if spikex2 < 0:
c.delete(spike)
spikes.remove(spike)
score += spike_score
c.itemconfig(score_text, text="Score: " + str(score))
if detect_collision(jumper, spike):
lose_a_life()
root.after(spike_speed, move_spikes)
def detect_collision(jumper, spike):
jumper_coords = c.coords(jumper)
spike_coords = c.coords(spike)
if jumper_coords[2] > spike_coords[0] and jumper_coords[0] < spike_coords[2]:
if jumper_coords[3] > spike_coords[1] and jumper_coords[1] < spike_coords[3]:
return True
return False
def spike_cleared(spike):
spikes.remove(spike)
c.delete(spike)
def lose_a_life():
global lives
lives -= 1
if lives == 0:
messagebox.showinfo("Game Over!", "Final Score: " + str(score))
root.destroy()
def move_up(event):
(x1, y1, x2, y2) = c.coords(jumper)
if y1 > 0:
c.move(jumper, 0, -20)
if y1 < 8:
c.move(jumper, 0, 20)
def move_down(event):
(x1, y1, x2, y2) = c.coords(jumper)
if y1 > 0:
c.move(jumper, 0, 20)
if y2 > 577:
c.move(jumper, 0, -20)
def move_left(event):
(x1, y1, x2, y2) = c.coords(jumper)
if x1 > 0:
c.move(jumper, -20, 0)
def move_right(event):
(x1, y1, x2, y2) = c.coords(jumper)
if x2 < canvas_width:
c.move(jumper, 20, 0)
c.bind("<Up>", move_up)
c.bind("<Down>", move_down)
c.bind("<Left>", move_left)
c.bind("<Right>", move_right)
c.focus_set()
root.after(1000, create_spike)
root.after(1000, move_spikes)
root.mainloop()
这些更改包括:
-
detect_collision
函数: 此函数已添加以专门检查玩家与尖刺之间的碰撞,使用两个对象的坐标。 -
move_spikes
函数: 此函数现在调用detect_collision
来检查是否有碰撞。如果是,则调用lose_a_life
函数。 - 分数增加: 当一个尖刺离开屏幕时,现在会增加分数。
- 代码清理: 一些不必要的代码已被删除或简化。
通过这些更改,该游戏现在应该能够按预期工作,正确检测碰撞和增加分数。
标签:python,tkinter,overlapping From: 78818141