gpt4 book ai didi

Python Snake 不长

转载 作者:行者123 更新时间:2023-12-04 07:32:33 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How do I chain the movement of a snake's body?

(1 个回答)


11 个月前关闭。




我是 python 新手,现在才开始学习基础知识,我正在尝试制作一个蛇游戏,但我似乎无法弄清楚如何让我的蛇每次吃一个苹果时多长 1 个正方形。我的理由是,我保留了所有旧位置的列表,但只在屏幕上显示最后一个位置,每次蛇吃另一个苹果时,它都会显示 1 个额外的旧位置,使蛇长 1 个方格。
这是我的代码:

import pygame
from random import randint

WIDTH = 400
HEIGHT = 300
dis = pygame.display.set_mode((WIDTH,HEIGHT))
white = (255,255,255)
BACKGROUND = white
blue = [0,0,255]
red = [255,0,0]

class Snake:
def __init__(self):
self.image = pygame.image.load("snake.bmp")
self.rect = self.image.get_rect()
self.position = (10,10)
self.direction = [0,0]
self.positionslist = [self.position]
self.length = 1
pygame.display.set_caption('Snake ')


def draw_snake(self,screen):
screen.blit(self.image, self.position)



def update(self):
self.position = (self.position[0] + self.direction[0],self.position[1] + self.direction[1])
self.rect = (self.position[0],self.position[1],5, 5 )
self.positionslist.append(self.position)
if self.position[0]< 0 :
self.position = (WIDTH,self.position[1])
elif self.position[0] > WIDTH:
self.position = (0,self.position[1])
elif self.position[1] > HEIGHT:
self.position = (self.position[0],0)
elif self.position[1] < 0 :
self.position = (self.position[0],HEIGHT)





class Food:
def __init__(self):
self.image = pygame.image.load("appel.png")
self.rect = self.image.get_rect()

apple_width = -self.image.get_width()
apple_height = -self.image.get_height()
self.position = (randint(0,WIDTH+apple_width-50),randint(0,HEIGHT+apple_height-50))
self.rect.x = self.position[0]
self.rect.y = self.position[1]

def draw_appel(self,screen):
screen.blit(self.image, self.position)

def eat_appel (self, snake):

if self.rect.colliderect(snake.rect) == True:

self.position = (randint(0,WIDTH),randint(0,HEIGHT))
self.rect.x = self.position[0]
self.rect.y = self.position[1]
snake.length += 1



def main():
game_over = False
while not game_over:
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
snake = Snake()
food = Food()

while True:
screen.fill(BACKGROUND)
font = pygame.font.Font('freesansbold.ttf', 12)
text = font.render(str(snake.length), True, red, white)
textRect = text.get_rect()
textRect.center = (387, 292)
screen.blit(text,textRect)
snake.update()

#snake.update_list()
snake.draw_snake(screen)
food.draw_appel(screen)
food.eat_appel(snake)
pygame.display.flip()

clock.tick(60)




for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
snake.direction = [0,1]
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
snake.direction = [1,0]
if event.key == pygame.K_LEFT:
snake.direction = [-1,0]
if event.key == pygame.K_UP:
snake.direction = [0,-1]
if event.key == pygame.K_DOWN:
snake.direction = [0,1]




if __name__ == "__main__":
main()

最佳答案

这可能有多种原因。首先,我看到您尝试通过键入snake.length += 1 来增加蛇的长度,这可能有效(可能不会,因为模块 pygame 允许蛇在周围盘旋,但不像循环或条件语句) .我的一个建议是,通过使用每次将分数与你当前的蛇长度相加的想法来增加蛇的长度(因为一旦你的分数是 1 通过吃苹果,你的蛇长度将是 2。并且随着分数的增加而增加)。这是我的代码(可能需要一些修改):

import pygame
import time
import random

pygame.init()

white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
orange = (255, 165, 0)

width, height = 600, 400

game_display = pygame.display.set_mode((width, height))
pygame.display.set_caption("Snake Mania")

clock = pygame.time.Clock()

snake_size = 10
snake_speed = 15

message_font = pygame.font.SysFont('ubuntu', 30)
score_font = pygame.font.SysFont('ubuntu', 25)

def print_score(score):
text = score_font.render("Score: " + str(score), True, orange)
game_display.blit(text, [0,0])

def draw_snake(snake_size, snake_pixels):
for pixel in snake_pixels:
pygame.draw.rect(game_display, white, [pixel[0], pixel[1], snake_size, snake_size])


def run_game():

game_over = False
game_close = False

x = width / 2
y = height / 2

x_speed = 0
y_speed = 0

snake_pixels = []
snake_length = 1

target_x = round(random.randrange(0, width - snake_size) / 10.0) * 10.0
target_y = round(random.randrange(0, height - snake_size) / 10.0) * 10.0

while not game_over:

while game_close:
game_display.fill(black)
game_over_message = message_font.render("Game Over!", True, red)
game_display.blit(game_over_message, [width / 3, height / 3])
print_score(snake_length - 1)
pygame.display.update()

for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
game_over = True
game_close = False
if event.key == pygame.K_2:
run_game()
if event.type == pygame.QUIT:
game_over = True
game_close = False

for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_speed = -snake_size
y_speed = 0
if event.key == pygame.K_RIGHT:
x_speed = snake_size
y_speed = 0
if event.key == pygame.K_UP:
x_speed = 0
y_speed = -snake_size
if event.key == pygame.K_DOWN:
x_speed = 0
y_speed = snake_size

if x >= width or x < 0 or y >= height or y < 0:
game_close = True

x += x_speed
y += y_speed

game_display.fill(black)
pygame.draw.rect(game_display, orange, [target_x, target_y, snake_size, snake_size])
snake_pixels.append([x, y])

if len(snake_pixels) > snake_length:
del snake_pixels[0]

for pixel in snake_pixels[:-1]:
if pixel == [x, y]:
game_close = True

draw_snake(snake_size, snake_pixels)
print_score(snake_length - 1)

pygame.display.update()

if x == target_x and y == target_y:
target_x = round(random.randrange(0, width - snake_size) / 10.0) * 10.0
target_y = round(random.randrange(0, height - snake_size) / 10.0) * 10.0
snake_length += 1

clock.tick(snake_speed)

pygame.quit()
quit()

run_game()

关于Python Snake 不长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67878050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com