Pygame中的贪吃蛇游戏,如何让苹果原地不动
Snake game in Pygame, how to make apple stay in the same place
我是使用 pygame 和 python 本身的初学者。我正在制作蛇游戏,但我不知道如何让苹果留在同一个地方。到目前为止,我制作了一个游戏板并开始了蛇。苹果正在产卵,但问题是它每秒都在改变它的位置,我不知道如何阻止它
到目前为止,这是我的代码:
import pygame
import sys
import random
pygame.init()
SPAWNSNAKE = pygame.USEREVENT #events
pygame.time.set_timer(SPAWNSNAKE, 800)
WHITE = (150, 150, 150)
RED = (255, 0, 0)
GREEN = (30, 50, 30) #colors
BLACK = (0, 0, 0)
FPS = 1
WIDTH = 500
HEIGHT = 500
DISPLAY = pygame.display.set_mode((WIDTH, HEIGHT))
DISPLAY.fill(GREEN)
grid_size = 25
snake_list = []
apple_list = []
grid_list = []
class Snake: #creates a starting snake
def __init__(self):
self.snake = [(0, 475), (25, 475), (50, 475), (75, 475), (100, 475)]
self.snake_width = grid_size
self.snake_height = grid_size
def create_snake(self):
for x, y in self.snake:
snake_rect = pygame.Rect(x, y, self.snake_width, self.snake_height)
snake_list.append(snake_rect)
def draw_snake(self):
for rectangle in snake_list:
pygame.draw.rect(DISPLAY, BLACK, rectangle)
def draw_grid(): #draws game board
for x in range(0, WIDTH, grid_size):
for y in range(0, HEIGHT, grid_size):
rect = pygame.Rect(x, y, grid_size, grid_size)
pygame.draw.rect(DISPLAY, WHITE, rect, 1)
grid_list.append(rect)
class Apples:
def create_apple(self): #creates an apple and adds it to the list
apple = random.choice(grid_list)
apple_list.append(apple)
def draw_apple(self): #draws that apple on screen
for apple_rectangle in apple_list:
pygame.draw.rect(DISPLAY, RED, apple_rectangle)
snake = Snake()
apples = Apples()
clock = pygame.time.Clock()
while True:
draw_grid()
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == SPAWNSNAKE:
snake.create_snake()
if len(apple_list) == 1: #always keeps apple count 1
apple_list = apple_list[1:]
DISPLAY.fill(GREEN) #refreshes display so that previous apples dissapears(works so far but that 1 apple changes it's place every second)
draw_grid()
print(apple_list)
apples.create_apple()
snake.draw_snake()
apples.draw_apple()
pygame.display.update()
我该怎么做才能让苹果总是在同一个地方?
了解 Classes and Instance Objects。不要创造 class 个苹果。为苹果创建 class。您可以从这个实例中创建任意数量的实例 class:
class Apple:
def __init__(self):
self.rect = random.choice(grid_list)
def draw(self):
pygame.draw.rect(DISPLAY, RED, self.rect)
创建苹果并将它们添加到应用程序循环之前的苹果列表中。在应用程序循环中绘制列表中的苹果:
apple_list = []
apple1 = Apple()
apple_list.append(apple1)
apple2 = Apple()
apple_list.append(apple2)
# [...]
while True:
# [...]
for apple in apple_list:
apple.draw()
# [...]
我是使用 pygame 和 python 本身的初学者。我正在制作蛇游戏,但我不知道如何让苹果留在同一个地方。到目前为止,我制作了一个游戏板并开始了蛇。苹果正在产卵,但问题是它每秒都在改变它的位置,我不知道如何阻止它
到目前为止,这是我的代码:
import pygame
import sys
import random
pygame.init()
SPAWNSNAKE = pygame.USEREVENT #events
pygame.time.set_timer(SPAWNSNAKE, 800)
WHITE = (150, 150, 150)
RED = (255, 0, 0)
GREEN = (30, 50, 30) #colors
BLACK = (0, 0, 0)
FPS = 1
WIDTH = 500
HEIGHT = 500
DISPLAY = pygame.display.set_mode((WIDTH, HEIGHT))
DISPLAY.fill(GREEN)
grid_size = 25
snake_list = []
apple_list = []
grid_list = []
class Snake: #creates a starting snake
def __init__(self):
self.snake = [(0, 475), (25, 475), (50, 475), (75, 475), (100, 475)]
self.snake_width = grid_size
self.snake_height = grid_size
def create_snake(self):
for x, y in self.snake:
snake_rect = pygame.Rect(x, y, self.snake_width, self.snake_height)
snake_list.append(snake_rect)
def draw_snake(self):
for rectangle in snake_list:
pygame.draw.rect(DISPLAY, BLACK, rectangle)
def draw_grid(): #draws game board
for x in range(0, WIDTH, grid_size):
for y in range(0, HEIGHT, grid_size):
rect = pygame.Rect(x, y, grid_size, grid_size)
pygame.draw.rect(DISPLAY, WHITE, rect, 1)
grid_list.append(rect)
class Apples:
def create_apple(self): #creates an apple and adds it to the list
apple = random.choice(grid_list)
apple_list.append(apple)
def draw_apple(self): #draws that apple on screen
for apple_rectangle in apple_list:
pygame.draw.rect(DISPLAY, RED, apple_rectangle)
snake = Snake()
apples = Apples()
clock = pygame.time.Clock()
while True:
draw_grid()
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == SPAWNSNAKE:
snake.create_snake()
if len(apple_list) == 1: #always keeps apple count 1
apple_list = apple_list[1:]
DISPLAY.fill(GREEN) #refreshes display so that previous apples dissapears(works so far but that 1 apple changes it's place every second)
draw_grid()
print(apple_list)
apples.create_apple()
snake.draw_snake()
apples.draw_apple()
pygame.display.update()
我该怎么做才能让苹果总是在同一个地方?
了解 Classes and Instance Objects。不要创造 class 个苹果。为苹果创建 class。您可以从这个实例中创建任意数量的实例 class:
class Apple:
def __init__(self):
self.rect = random.choice(grid_list)
def draw(self):
pygame.draw.rect(DISPLAY, RED, self.rect)
创建苹果并将它们添加到应用程序循环之前的苹果列表中。在应用程序循环中绘制列表中的苹果:
apple_list = []
apple1 = Apple()
apple_list.append(apple1)
apple2 = Apple()
apple_list.append(apple2)
# [...]
while True:
# [...]
for apple in apple_list:
apple.draw()
# [...]