Python 元组以随机间隔停止更新
我正在尝试制作一个蛇类游戏,有时它可以工作,我基本上可以附加一块8次,然后游戏停止工作。然后,我再次运行它,它在崩溃之前工作一两次。我尝试过创建一些方法,使添加到蛇身上更容易,但我似乎无法始终让它工作。下面是对代码的回复。非常感谢您的帮助Python 元组以随机间隔停止更新,python,class,user-interface,Python,Class,User Interface,我正在尝试制作一个蛇类游戏,有时它可以工作,我基本上可以附加一块8次,然后游戏停止工作。然后,我再次运行它,它在崩溃之前工作一两次。我尝试过创建一些方法,使添加到蛇身上更容易,但我似乎无法始终让它工作。下面是对代码的回复。非常感谢您的帮助 ##### SNAKE.PY ##### from turtle import * from random import * # noinspection PyShadowingNames class Snake: ""&quo
##### SNAKE.PY #####
from turtle import *
from random import *
# noinspection PyShadowingNames
class Snake:
"""Class that creates the initial snake object"""
def __init__(self):
# Make starting point of snake game
self.start_positions = [(0, 0), (-20, 0), (-40, 0)]
# Used to store created parts, the snakes body
self.snake_body = []
for position in self.start_positions:
# Create 3 white squares
t = Turtle('square')
t.penup()
t.speed('normal')
t.color('white')
# Send them to the starting spot
t.goto(position)
# Add to snake body
self.snake_body.append(t)
# Declare first part as head for ease of use
self.snake_head = self.snake_body[0]
def move(self):
"""Function used to move all the snake body parts forward"""
for part in range(len(self.snake_body) - 1, 0, -1):
# Loop through parts backwards to get them to move at same time
new_x = self.snake_body[part - 1].xcor()
new_y = self.snake_body[part - 1].ycor()
self.snake_body[part].goto(new_x, new_y)
self.snake_head.fd(10)
def up(self):
"""Sets snake positioning north"""
self.snake_head.seth(90)
def down(self):
"""Sets snake positioning south"""
self.snake_head.seth(270)
def left(self):
"""Sets snake positioning west"""
self.snake_head.seth(180)
def right(self):
"""Sets snake positioning east"""
self.snake_head.seth(0)
def add_part(self):
"""Function to add a new part"""
t = Turtle('square')
t.penup()
t.speed('normal')
t.color('white')
self.snake_body.append(t)
class Parts:
"""Class to create dots to add to the snakes length"""
def __init__(self, screen):
self.screen = screen
self.dot = Turtle('square')
self.dot.color('white')
self.dot.penup()
def generate_dot_cor(self):
x = randrange(-280, 280, 10)
y = randrange(-280, 280, 10)
return x,y
####### MAIN.PY #######
from turtle import *
from snake import Snake, Parts
import time
# Screen setup
screen = Screen()
screen.setup(600, 600)
screen.bgcolor('black')
screen.title('My Snake Game')
screen.tracer(0)
# Call the snake class from the snake.py file
snake = Snake()
# Call the Parts class from the snake.py file
dot = Parts(screen)
dot.dot.goto(dot.generate_dot_cor())
# Keep track of player score
score = 0
# Set initial coordinates
coors = dot.generate_dot_cor()
target = []
# Set loop flag
game_on = True
while game_on:
# Update the screen every loop to move snake forward
screen.update()
time.sleep(0.05)
# Method from snake class that continuously moves the snake
snake.move()
if snake.snake_head.pos() == dot.dot.pos():
snake.add_part()
new_coors = dot.generate_dot_cor()
print(new_coors)
dot.dot.goto(new_coors)
# Allows user to rotate snake head depending on the button pressed
screen.onkey(snake.up, 'Up')
screen.onkey(snake.right, 'Right')
screen.onkey(snake.down, 'Down')
screen.onkey(snake.left, 'Left')
screen.listen()
screen.exitonclick()
Edit: Changed format of code
您必须在问题中提供文本格式的代码,还必须提供一个最小的可复制示例,而不是整个代码(这也是可测试的,意味着我们可以在没有问题的情况下运行它,但它很简洁)@马蒂斯:是这样的吗?这当然更好,我建议在你的帖子中保留完整的代码,但你也必须提供一个最小的可复制示例,请阅读这里: