简体   繁体   English

python中的贪吃蛇游戏,屏幕上没有出现食物

[英]Snake game in python, the food does not appear on the screen

I am trying to create a snake game with Turtle graphics.我正在尝试使用 Turtle 图形创建蛇游戏。 The food is a class that inherits the Turtle but when I run the code is does not appear on the screen:食物是继承了 Turtle 的 class,但是当我运行代码时,屏幕上没有出现:

food.py食物.py

from turtle import Turtle
import random

class Food(Turtle):

    def __int__(self):
        super().__init__()
        self.shape("circle")
        self.penup()
        self.shapesize(stretch_len=0.5, stetch_wid=0.5)
        self.color("blue")
        random_x = random.randint(-280,280)
        random_y = random.randint(-280, 280)
        self.goto(random_x, random_y)
        print(random_x,random_y)

snake.py蛇.py

from turtle import Turtle

STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20
UP = 90
DOWN = 270
RIGHT = 0
LEFT = 180


class Snake:

    def __init__(self):
        self.segments = []
        self.create_snake()

    def create_snake(self):
        for position in STARTING_POSITIONS:
            new_segment = Turtle(shape="square")
            new_segment.color("white")
            new_segment.pu()
            new_segment.setpos(position)
            self.segments.append(new_segment)

    def move(self):
        for seg_num in range((len(self.segments) - 1), 0, -1):
            new_x = self.segments[seg_num - 1].xcor()
            new_y = self.segments[seg_num - 1].ycor()
            self.segments[seg_num].goto(new_x, new_y)
        self.segments[0].forward(MOVE_DISTANCE)

    def up(self):
        if self.segments[0].heading() != DOWN:
            self.segments[0].setheading(UP)

    def down(self):
        if self.segments[0].heading() != UP:
            self.segments[0].setheading(DOWN)

    def left(self):
        if self.segments[0].heading() != RIGHT:
            self.segments[0].setheading(LEFT)

    def right(self):
        if self.segments[0].heading() != LEFT:
            self.segments[0].setheading(RIGHT)e

main.py主程序

from turtle import Screen
from snake import Snake
from food import Food
import time

screen = Screen()
screen.setup(width=600,height=600)
screen.bgcolor("black")
screen.title("My Snake Game")
screen.tracer(0)

food = Food()
snake = Snake()


screen.listen()
screen.onkey(snake.up,"Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")

game_is_on = True
while game_is_on:
    screen.update()
    time.sleep(0.1)
    snake.move()

screen.exitonclick()

your text

When I created the food in the main.py file it was created, but when I tried to import it, it does not get created The screen and the snake are created, the snake get commands from thekeyboard.当我在 main.py 文件中创建食物时,它被创建了,但是当我尝试导入它时,它没有被创建。屏幕和蛇被创建,蛇从键盘获取命令。 I tried running it with Thonny, I saw that the food1 variable was allocated a memory, but when i performed debug and tried to "step into" Food(), it did not go into the class我尝试用 Thonny 运行它,我看到 food1 变量被分配了一个 memory,但是当我执行调试并试图“进入”Food() 时,它没有将 go 分配到 class

Here is the actual working code.这是实际的工作代码。
food.py食物.py

from turtle import Turtle
import random

class Food(Turtle):

    def __init__(self):
        super().__init__(shape="circle")
        self.penup()
        self.shapesize(stretch_len=0.5, stretch_wid=0.5)
        self.color("blue")
        self.move_to_new_location()

    def move_to_new_location(self):
        random_x = random.randint(-280, 280)
        random_y = random.randint(-280, 280)
        self.goto(random_x, random_y)

snake.py蛇.py

from turtle import Turtle

STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20
UP = 90
DOWN = 270
RIGHT = 0
LEFT = 180


class Snake:

    def __init__(self):
        self.segments = []
        self.create_snake()

    def create_snake(self):
        for position in STARTING_POSITIONS:
            new_segment = Turtle(shape="square")
            new_segment.color("white")
            new_segment.pu()
            new_segment.setpos(position)
            self.segments.append(new_segment)

    def move(self):
        for seg_num in range((len(self.segments) - 1), 0, -1):
            new_x = self.segments[seg_num - 1].xcor()
            new_y = self.segments[seg_num - 1].ycor()
            self.segments[seg_num].goto(new_x, new_y)
        self.segments[0].forward(MOVE_DISTANCE)

    def up(self):
        if self.segments[0].heading() != DOWN:
            self.segments[0].setheading(UP)

    def down(self):
        if self.segments[0].heading() != UP:
            self.segments[0].setheading(DOWN)

    def left(self):
        if self.segments[0].heading() != RIGHT:
            self.segments[0].setheading(LEFT)

    def right(self):
        if self.segments[0].heading() != LEFT:
            self.segments[0].setheading(RIGHT)

main.py主程序

import time
from turtle import Screen

from food import Food
from snake import Snake


def play_game():
    screen = Screen()
    screen.setup(width=600,height=600)
    screen.bgcolor("black")
    screen.title("My Snake Game")
    screen.tracer(0)

    food = Food()
    screen.addshape("circle", ((-5, -5), (-5, 5), (5, 5), (5, -5)))
    snake = Snake()

    screen.listen()
    screen.onkey(snake.up,"Up")
    screen.onkey(snake.down, "Down")
    screen.onkey(snake.left, "Left")
    screen.onkey(snake.right, "Right")

    game_is_on = True
    while game_is_on:
        screen.update()
        time.sleep(0.1)
        snake.move()
        if snake.segments[0].distance(food) < 20:
            food.move_to_new_location()
        if snake.segments[0].xcor() > 280 or snake.segments[0].xcor() < -280 or snake.segments[0].ycor() > 280 or snake.segments[0].ycor() < -280:
            game_is_on = False

    screen.exitonclick()


def main():
    play_game()


if __name__ == "__main__":
    main()

There is two typo mistakes in your code.您的代码中有两个拼写错误。

  1. In Food class must be function __init__ instead of __int__ .Food中 class 必须是 function __init__而不是__int__

  2. In Food.__init__ in line self.shapesize(stretch_len=0.5, stetch_wid=0.5) in second param stetch_wid you lost r .Food.__init__行中的self.shapesize(stretch_len=0.5, stetch_wid=0.5)第二个参数stetch_wid你丢失了r it must be stretch_wid .它必须是stretch_wid

And now you can see the food.现在你可以看到食物了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM