求助
import pygame,sys,randomfrom pygame.locals import *
red_color = pygame.Color(255,0,0)
blue_color = pygame.Color(0,0,0)
green_color = pygame.Color(255,255,255)
def game_over():
pygame.quit()
sys.exit()
def main():
pygame.init()
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption('贪吃蛇')
clock = pygame.time.Clock()
snake_position =
snake_body = [,,]
food_position =
food_flag = 1
direction = 'right'
key_direction
while True:
for event in pygame.event.get():
if event.type == QUIT:
game_over()
elif event.type == KEYDOWN:
if event.key == K_RIGHT:
key_direction = 'right'
if event.key == K_LEET:
key_direction ='left'
if event.key == K_UP:
key_direction = 'up'
if event.key == K_DOWN:
key_direction = 'down'
if event.key == k_ESCAPE:
pygame.event.post(pygame.event.Event(OUIT))
if key_direction == 'left'and not direction == 'right':
direction = key_direction
if key_direction == 'right'and not direction == 'right':
direction = key_direction
if key_direction == 'up'and not direction == 'right':
direction = key_direction
if key_direction == 'down'and not direction == 'right':
direction = key_direction
if direction == 'right':
snake_position += 20
if direction =='left':
snake_position -= 20
if direction =='up':
snake_position -= 20
if direction =='down':
snake_position += 20
snake_body.insert(0,list(snake_position))
if snake_position == food_position and
snake_position == food_position:
food_flag = 0
else:
snake_body.pop()
if food_flag == 0:
x = random.randrange(0,640,20)
y = random.randrange(0,480,20)
food_position =
food_flag = 1
screen.fill(blue_color)
for position in snake_body:
pygame.draw.rect(screen,red_color,Rect(position,position,20,20))
pygame.draw.rect(screen,green_color,Rect(position,position,20,20))
pygame.display.flip()
if snake_position > 620 or snake_position < 0 :
game_over()
elif snake_position > 620 or snake_position < 0 :
game_over()
clock.tick(2)
if __name__ == '__main__':
main() 鱼油,你的代码有几个地方需要修改,才能让你的小蛇游戏正常运行。以下是更正后的代码:
import pygame, sys, random
from pygame.locals import *
red_color = pygame.Color(255, 0, 0)
blue_color = pygame.Color(0, 0, 255)
green_color = pygame.Color(0, 255, 0)
def game_over():
pygame.quit()
sys.exit()
def main():
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('贪吃蛇')
clock = pygame.time.Clock()
# 初始化蛇的位置和身体
snake_position =
snake_body = [, , ]
food_position =
food_flag = 1
direction = 'RIGHT'
key_direction = direction
while True:
for event in pygame.event.get():
if event.type == QUIT:
game_over()
elif event.type == KEYDOWN:
if event.key == K_RIGHT:
key_direction = 'RIGHT'
if event.key == K_LEFT:
key_direction = 'LEFT'
if event.key == K_UP:
key_direction = 'UP'
if event.key == K_DOWN:
key_direction = 'DOWN'
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
if key_direction == 'LEFT' and direction != 'RIGHT':
direction = key_direction
if key_direction == 'RIGHT' and direction != 'LEFT':
direction = key_direction
if key_direction == 'UP' and direction != 'DOWN':
direction = key_direction
if key_direction == 'DOWN' and direction != 'UP':
direction = key_direction
if direction == 'RIGHT':
snake_position += 20
if direction == 'LEFT':
snake_position -= 20
if direction == 'UP':
snake_position -= 20
if direction == 'DOWN':
snake_position += 20
snake_body.insert(0, list(snake_position))
if snake_position == food_position:
food_flag = 0
else:
snake_body.pop()
if food_flag == 0:
x = random.randrange(1, 32) * 20
y = random.randrange(1, 24) * 20
food_position =
food_flag = 1
screen.fill(blue_color)
for position in snake_body:
pygame.draw.rect(screen, red_color, Rect(position, position, 20, 20))
pygame.draw.rect(screen, green_color, Rect(food_position, food_position, 20, 20))
pygame.display.flip()
if snake_position >= 640 or snake_position < 0 or snake_position >= 480 or snake_position < 0:
game_over()
clock.tick(10)
if __name__ == '__main__':
main()
主要的修改如下:
1. `snake_position` 和 `snake_body` 进行正确的初始化。
2. 修正对按键事件的处理,例如 `K_LEET` 改为 `K_LEFT`。
3. 修正方向与移动逻辑,使其正常工作并避免画面错误。
4. 修正食物位置和食物标志的更新。
5. 在边界检测时,确保所有的边界条件都可以正常退出游戏。
这样,你的小蛇游戏应该就可以正常运行了。希望这个解答能帮到你!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 求助内容说一下啊
页:
[1]