弧形奇怪地出来
Arc shape coming out strangely
我正在尝试做一个接鸡蛋的游戏,除了篮筐外一切正常,它应该看起来像一个倒转的弧形但它变成了一个非常奇怪的形状,我已经搜索了很长时间尽我所能,但没有人遇到过这样的问题。
我希望它看起来像这样:
我必须post整个程序,否则你不会理解:
from itertools import cycle
from random import randrange
from tkinter import Canvas, Tk, messagebox, font
canvas_width = 800
canvas_height = 400
root = Tk()
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_factor = 0.95
catcher_color = "blue"
catcher_width = 100
catcher_height = 100
catcher_start_x = canvas_width / 2 - catcher_width / 2
catcher_start_y = canvas_height - catcher_width - 20
catcher_start_x2 = catcher_start_x = catcher_width
catcher_start_y2 = catcher_start_y + catcher_height
catcher = c.create_arc(
catcher_start_x,
catcher_start_y,
catcher_start_x2,
catcher_start_y2,
start=200,
extent=140,
style="arc",
outline=catcher_color,
width=30
)
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:
(egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
c.move(egg, 0, 10)
if egg_y2 > canvas_height:
egg_dropped(egg)
root.after(egg_speed, move_eggs)
def egg_dropped(egg):
eggs.remove(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():
catcher_x, catcher_y, catcher_x2, catcher_y2 = c.coords(catcher)
for egg in eggs:
egg_x, egg_y, egg_x2, egg_y2 = c.coords(egg)
if egg_x<catcher_x<egg_x2 and (catcher_y2 - egg_y2) < 5:
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_factor)
egg_interval = int(egg_interval * difficulty_factor)
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()
您在第 27 行有错字 - 您在本应 catcher_start_x2 = catcher_start_x - catcher_width
.
的地方写了 catcher_start_x2 = catcher_start_x = catcher_width
此外,第 29 行的反斜杠是不必要的,因为括号中的内容会自动在多行中继续。
还有一件事 - 一旦拼写错误被修复,鸡蛋只会在捕手的左侧区域被捕获,因为第 79 行(if egg_x<catcher_x<egg_x2 and (catcher_y2 - egg_y2) < 5:
)正在检查捕手的左侧 x 坐标是否在捕手的内部鸡蛋的 x 坐标不是相反。要解决此问题,请将其替换为
if catcher_x - 10 < egg_x and egg_x2 < catcher_x2 + 10 and (catcher_y2 - egg_y2) < 5:
(-10 和 +10 是为了给玩家一些余地,这样鸡蛋就不必正好在中心)。
我正在尝试做一个接鸡蛋的游戏,除了篮筐外一切正常,它应该看起来像一个倒转的弧形但它变成了一个非常奇怪的形状,我已经搜索了很长时间尽我所能,但没有人遇到过这样的问题。
我希望它看起来像这样:
我必须post整个程序,否则你不会理解:
from itertools import cycle
from random import randrange
from tkinter import Canvas, Tk, messagebox, font
canvas_width = 800
canvas_height = 400
root = Tk()
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_factor = 0.95
catcher_color = "blue"
catcher_width = 100
catcher_height = 100
catcher_start_x = canvas_width / 2 - catcher_width / 2
catcher_start_y = canvas_height - catcher_width - 20
catcher_start_x2 = catcher_start_x = catcher_width
catcher_start_y2 = catcher_start_y + catcher_height
catcher = c.create_arc(
catcher_start_x,
catcher_start_y,
catcher_start_x2,
catcher_start_y2,
start=200,
extent=140,
style="arc",
outline=catcher_color,
width=30
)
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:
(egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
c.move(egg, 0, 10)
if egg_y2 > canvas_height:
egg_dropped(egg)
root.after(egg_speed, move_eggs)
def egg_dropped(egg):
eggs.remove(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():
catcher_x, catcher_y, catcher_x2, catcher_y2 = c.coords(catcher)
for egg in eggs:
egg_x, egg_y, egg_x2, egg_y2 = c.coords(egg)
if egg_x<catcher_x<egg_x2 and (catcher_y2 - egg_y2) < 5:
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_factor)
egg_interval = int(egg_interval * difficulty_factor)
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()
您在第 27 行有错字 - 您在本应 catcher_start_x2 = catcher_start_x - catcher_width
.
catcher_start_x2 = catcher_start_x = catcher_width
此外,第 29 行的反斜杠是不必要的,因为括号中的内容会自动在多行中继续。
还有一件事 - 一旦拼写错误被修复,鸡蛋只会在捕手的左侧区域被捕获,因为第 79 行(if egg_x<catcher_x<egg_x2 and (catcher_y2 - egg_y2) < 5:
)正在检查捕手的左侧 x 坐标是否在捕手的内部鸡蛋的 x 坐标不是相反。要解决此问题,请将其替换为
if catcher_x - 10 < egg_x and egg_x2 < catcher_x2 + 10 and (catcher_y2 - egg_y2) < 5:
(-10 和 +10 是为了给玩家一些余地,这样鸡蛋就不必正好在中心)。