鱼C论坛

 找回密码
 立即注册
查看: 113|回复: 2

[已解决]求助

[复制链接]
发表于 2024-8-28 18:24:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import pygame,sys,random
from 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 = [100,100]
    snake_body = [[100,100],[80,100],[60,100]]
    food_position = [300,300]
    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[0] += 20
        if direction =='left':
            snake_position[0] -= 20
        if direction =='up':
            snake_position[0] -= 20
        if direction =='down':
            snake_position[0] += 20
        snake_body.insert(0,list(snake_position))
        if snake_position[0] == food_position[0] and
            snake_position[1] == food_position[1]:
                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 = [x,y]
            food_flag = 1
        screen.fill(blue_color)
        for position in snake_body:
            pygame.draw.rect(screen,red_color,Rect(position[0],position[1],20,20))
        pygame.draw.rect(screen,green_color,Rect(position[0],position[1],20,20))
        pygame.display.flip()
        if snake_position[0] > 620 or snake_position[0] < 0 :
            game_over()
        elif snake_position[0] > 620 or snake_position[0] < 0 :
            game_over()
        clock.tick(2)

        if __name__ == '__main__':
            main()
最佳答案
2024-8-28 18:34:04
鱼油,你的代码有几个地方需要修改,才能让你的小蛇游戏正常运行。以下是更正后的代码:
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 = [100, 100]
    snake_body = [[100, 100], [80, 100], [60, 100]]
    food_position = [random.randrange(1, 32) * 20, random.randrange(1, 24) * 20]
    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[0] += 20
        if direction == 'LEFT':
            snake_position[0] -= 20
        if direction == 'UP':
            snake_position[1] -= 20
        if direction == 'DOWN':
            snake_position[1] += 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 = [x, y]
            food_flag = 1

        screen.fill(blue_color)
        for position in snake_body:
            pygame.draw.rect(screen, red_color, Rect(position[0], position[1], 20, 20))
        pygame.draw.rect(screen, green_color, Rect(food_position[0], food_position[1], 20, 20))

        pygame.display.flip()

        if snake_position[0] >= 640 or snake_position[0] < 0 or snake_position[1] >= 480 or snake_position[1] < 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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-8-28 18:34:04 | 显示全部楼层    本楼为最佳答案   
鱼油,你的代码有几个地方需要修改,才能让你的小蛇游戏正常运行。以下是更正后的代码:
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 = [100, 100]
    snake_body = [[100, 100], [80, 100], [60, 100]]
    food_position = [random.randrange(1, 32) * 20, random.randrange(1, 24) * 20]
    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[0] += 20
        if direction == 'LEFT':
            snake_position[0] -= 20
        if direction == 'UP':
            snake_position[1] -= 20
        if direction == 'DOWN':
            snake_position[1] += 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 = [x, y]
            food_flag = 1

        screen.fill(blue_color)
        for position in snake_body:
            pygame.draw.rect(screen, red_color, Rect(position[0], position[1], 20, 20))
        pygame.draw.rect(screen, green_color, Rect(food_position[0], food_position[1], 20, 20))

        pygame.display.flip()

        if snake_position[0] >= 640 or snake_position[0] < 0 or snake_position[1] >= 480 or snake_position[1] < 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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-29 21:35:17 | 显示全部楼层
求助内容说一下啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-17 03:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表