【发布时间】:2022-01-22 21:23:45
【问题描述】:
我不知道为什么,但是在画布的左侧和顶部,我在使用海龟创建我的蛇游戏时出现了奇怪的行为。左侧和顶部有一个 10 像素的边框。
主代码
import functools
from turtle import Screen
import time
from Snake import Snake
# initialize screen
win = Screen()
WIDTH = HEIGHT = 800
win.setup(width=WIDTH, height=HEIGHT)
win.bgcolor("black")
canvas = win.getcanvas()
root = canvas.winfo_toplevel()
root.overrideredirect(1)
win.tracer(0)
s = Snake()
def main():
segments = s.init_segments()
win.update()
move_keys = ["Right", "Up", "Left", "Down"]
running = True
inc = 0
for k in move_keys:
win.onkeypress(functools.partial(s.move, segments, inc, k), key=k)
inc += 90
win.listen()
while running:
win.update()
time.sleep(0.1)
s.move(segments, segments[0].heading(), "none")
if s.crashed(win.window_width(), win.window_height(), segments):
running = False
break
win.exitonclick()
main()
蛇码
from turtle import Turtle
class Snake:
def init_segments(self):
segs = []
x_pos = 0
for i in range(3):
t = Turtle(shape="square")
t.color("white")
t.penup()
t.speed(1)
segs.append(t)
if t != segs[0]:
x_pos -= 20
t.goto(x_pos, 0)
return segs
def __no_turn(self, h, d):
if h - d == -180 or h - d == 180:
return True
else:
return False
def __no_skip(self, k, dir):
if (k == "Right" and dir == 0 or k == "Up" and dir == 90
or k == "Left" and dir == 180 or k == "Down" and dir == 270):
return False
else:
return True
def move(self, t_list , dir, k):
for seg_num in range(len(t_list) - 1, -1, -1):
if self.__no_turn(t_list[0].heading(), dir):
continue
elif seg_num > 0:
if self.__no_skip(k, dir):
t_list[seg_num].goto(t_list[seg_num - 1].position())
else:
t_list[seg_num].setheading(dir)
if self.__no_skip(k, dir):
t_list[seg_num].forward(20)
def crashed(self, window_width, window_height, t_list):
head_position = t_list[0].position()
body_positions = []
hw = window_width/2
hh = window_height/2
for i in range(len(t_list)):
body_positions.append(t_list[i].position())
body_positions.pop(0)
for i in range(len(body_positions)):
if head_position == body_positions[i]:
return True
if head_position[0] >= hw:
print(f"Head Position: {head_position[0]} ... Wall Position: {hw}")
return True
elif head_position[0] <= -hw:
print(f"Head Position: {head_position[0]} ... Wall Position: {-hw}")
return True
elif head_position[1] >= hh:
print(f"Head Position: {head_position[1]} ... Wall Position: {hh}")
return True
elif head_position[1] <= -hh:
print(f"Head Position: {head_position[1]} ... Wall Position: {-hh}")
return True
添加了此文件中的打印行以尝试找出发生了什么。
请帮助我了解发生了什么。
谢谢。
编辑: 另外我之前没有注意到,但是底部和右侧有一个1px的边框。
第二次编辑以进一步说明问题。
我不久前添加了药丸的代码。我所做的是,我一次生成所有药丸并隐藏它们,因为你无法在不清除屏幕的情况下摧毁乌龟,而且每次都获取乌龟的位置并在屏幕上重新绘制乌龟的位置会很奇怪之前是 0.1 秒。然后我得到了这个结果。
如您所见,药丸正在屏幕外生成。
以下是我用来生成所有药丸的代码,类似于我用来确定屏幕边缘的代码。
hw = int(WIDTH/2)
hh = int(HEIGHT/2)
p_list = []
for i in range(-hw, hw, 20):
for j in range(-hh, hh, 20):
p = Pill((i, j))
p.show_pill(segments)
p_list.append(p)
当我重新订购 win.update() 时会发生什么
正如您在此处看到的,右侧现在将整个片段压入其中。如果您仔细观察,您会在这张图片中看到蛇的白色覆盖了未覆盖的空间的额外像素上一张图片,它停在圆圈的最后一个像素上。 您还会在第二张图片和这张图片中看到,底部有一排圆圈从窗口边框的边缘向外窥视。
【问题讨论】:
标签: python-3.x turtle-graphics