贪吃蛇编程简易代码是什么

fiy 其他 55

回复

共3条回复 我来回复
  • 不及物动词的头像
    不及物动词
    这个人很懒,什么都没有留下~
    评论

    贪吃蛇是一款经典的游戏,编程实现它可以锻炼我们的逻辑思维和编程能力。下面是一个简易的贪吃蛇游戏的代码:

    import pygame
    import time
    import random
    
    # 初始化pygame
    pygame.init()
    
    # 定义颜色
    white = (255, 255, 255)
    yellow = (255, 255, 102)
    black = (0, 0, 0)
    red = (213, 50, 80)
    green = (0, 255, 0)
    blue = (50, 153, 213)
    
    # 设置游戏窗口的宽度和高度
    dis_width = 800
    dis_height = 600
    dis = pygame.display.set_mode((dis_width, dis_height))
    pygame.display.set_caption('贪吃蛇游戏')
    
    # 设置游戏时钟
    clock = pygame.time.Clock()
    
    snake_block = 10
    snake_speed = 30
    
    font_style = pygame.font.SysFont(None, 50)
    score_font = pygame.font.SysFont(None, 35)
    
    def our_snake(snake_block, snake_list):
        for x in snake_list:
            pygame.draw.rect(dis, green, [x[0], x[1], snake_block, snake_block])
    
    def game_loop():
        game_over = False
        game_close = False
    
        # 初始化贪吃蛇的位置和长度
        x1 = dis_width / 2
        y1 = dis_height / 2
        x1_change = 0
        y1_change = 0
    
        snake_List = []
        Length_of_snake = 1
    
        # 设置随机生成食物的位置
        foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
        foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
    
        while not game_over:
            while game_close:
                dis.fill(black)
                message("游戏结束!按Q-退出或者C-重新开始", red)
                pygame.display.update()
    
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        game_over = True
                        game_close = False
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_q:
                            game_over = True
                            game_close = False
                        if event.key == pygame.K_c:
                            game_loop()
    
            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:
                        x1_change = -snake_block
                        y1_change = 0
                    elif event.key == pygame.K_RIGHT:
                        x1_change = snake_block
                        y1_change = 0
                    elif event.key == pygame.K_UP:
                        y1_change = -snake_block
                        x1_change = 0
                    elif event.key == pygame.K_DOWN:
                        y1_change = snake_block
                        x1_change = 0
    
            if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
                game_close = True
    
            x1 += x1_change
            y1 += y1_change
            dis.fill(black)
            pygame.draw.rect(dis, blue, [foodx, foody, snake_block, snake_block])
            snake_Head = []
            snake_Head.append(x1)
            snake_Head.append(y1)
            snake_List.append(snake_Head)
            if len(snake_List) > Length_of_snake:
                del snake_List[0]
    
            for x in snake_List[:-1]:
                if x == snake_Head:
                    game_close = True
    
            our_snake(snake_block, snake_List)
            pygame.display.update()
    
            if x1 == foodx and y1 == foody:
                foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
                foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
                Length_of_snake += 1
    
            clock.tick(snake_speed)
    
        pygame.quit()
        quit()
    
    def message(msg, color):
        mesg = font_style.render(msg, True, color)
        dis.blit(mesg, [dis_width / 6, dis_height / 3])
    
    game_loop()
    

    上述代码使用Pygame库来实现贪吃蛇游戏。首先,我们需要导入pygame库并进行初始化。然后,定义游戏窗口的大小和颜色。接下来,我们设置游戏时钟和贪吃蛇的大小和速度。

    在游戏循环中,我们初始化贪吃蛇的位置和长度,并设置食物的随机位置。然后,我们监听键盘事件,并根据按键的方向改变贪吃蛇的移动方向。如果贪吃蛇碰到边界或者自己的身体,游戏结束。如果贪吃蛇吃到食物,长度增加。

    最后,我们使用pygame的绘图函数来绘制贪吃蛇和食物,并更新游戏窗口。当游戏结束时,显示游戏结束的消息,并根据按键的不同选择退出游戏或重新开始。

    以上就是一个简易的贪吃蛇游戏的代码实现。你可以根据自己的需求进行修改和扩展,添加更多的功能和特效。祝你编程愉快!

    1年前 0条评论
  • fiy的头像
    fiy
    Worktile&PingCode市场小伙伴
    评论

    贪吃蛇是一种经典的游戏,可以通过编程实现。下面是一个简易的贪吃蛇编程代码示例:

    import pygame
    import time
    import random
    
    pygame.init()
    
    white = (255, 255, 255)
    yellow = (255, 255, 102)
    black = (0, 0, 0)
    red = (213, 50, 80)
    green = (0, 255, 0)
    blue = (50, 153, 213)
    
    dis_width = 800
    dis_height = 600
    
    dis = pygame.display.set_mode((dis_width, dis_height))
    pygame.display.set_caption('贪吃蛇小游戏')
    
    clock = pygame.time.Clock()
    
    snake_block = 10
    snake_speed = 30
    
    font_style = pygame.font.SysFont(None, 50)
    score_font = pygame.font.SysFont(None, 35)
    
    
    def our_snake(snake_block, snake_list):
        for x in snake_list:
            pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
    
    
    def message(msg, color):
        mesg = font_style.render(msg, True, color)
        dis.blit(mesg, [dis_width / 6, dis_height / 3])
    
    
    def gameLoop():
        game_over = False
        game_close = False
    
        x1 = dis_width / 2
        y1 = dis_height / 2
    
        x1_change = 0
        y1_change = 0
    
        snake_List = []
        Length_of_snake = 1
    
        foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
        foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
    
        while not game_over:
    
            while game_close == True:
                dis.fill(blue)
                message("You Lost! Press Q-Quit or C-Play Again", red)
                pygame.display.update()
    
                for event in pygame.event.get():
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_q:
                            game_over = True
                            game_close = False
                        if event.key == pygame.K_c:
                            gameLoop()
    
            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:
                        x1_change = -snake_block
                        y1_change = 0
                    elif event.key == pygame.K_RIGHT:
                        x1_change = snake_block
                        y1_change = 0
                    elif event.key == pygame.K_UP:
                        y1_change = -snake_block
                        x1_change = 0
                    elif event.key == pygame.K_DOWN:
                        y1_change = snake_block
                        x1_change = 0
    
            if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
                game_close = True
            x1 += x1_change
            y1 += y1_change
            dis.fill(blue)
            pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block])
            snake_Head = []
            snake_Head.append(x1)
            snake_Head.append(y1)
            snake_List.append(snake_Head)
            if len(snake_List) > Length_of_snake:
                del snake_List[0]
    
            for x in snake_List[:-1]:
                if x == snake_Head:
                    game_close = True
    
            our_snake(snake_block, snake_List)
    
            pygame.display.update()
    
            if x1 == foodx and y1 == foody:
                foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
                foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
                Length_of_snake += 1
    
            clock.tick(snake_speed)
    
        pygame.quit()
    
    
    gameLoop()
    

    这个代码使用了pygame库来实现贪吃蛇游戏。游戏窗口的大小为800×600像素,蛇的大小为10像素,初始速度为30帧/秒。

    游戏开始时,蛇位于窗口的中间位置,通过键盘的方向键来控制蛇的移动方向。蛇吃到食物后,长度会增加,并在窗口中随机生成新的食物。

    游戏结束的条件有两个:蛇撞到了窗口边界或者蛇撞到了自己。当游戏结束时,可以选择重新开始游戏或者退出游戏。

    以上是一个简易的贪吃蛇游戏编程代码示例,你可以基于此代码进行修改和扩展,添加更多的功能和特性,使游戏更加丰富和有趣。

    1年前 0条评论
  • worktile的头像
    worktile
    Worktile官方账号
    评论

    下面是一个简易的贪吃蛇游戏的Python代码:

    import pygame
    import time
    import random
    
    # 初始化pygame
    pygame.init()
    
    # 定义颜色
    white = (255, 255, 255)
    yellow = (255, 255, 102)
    black = (0, 0, 0)
    red = (213, 50, 80)
    green = (0, 255, 0)
    blue = (50, 153, 213)
    
    # 设置屏幕宽高
    dis_width = 800
    dis_height = 600
    
    # 创建屏幕对象
    dis = pygame.display.set_mode((dis_width, dis_height))
    pygame.display.set_caption('贪吃蛇游戏')
    
    # 设置游戏速度
    clock = pygame.time.Clock()
    
    snake_block = 10
    snake_speed = 30
    
    font_style = pygame.font.SysFont(None, 50)
    score_font = pygame.font.SysFont(None, 35)
    
    
    def our_snake(snake_block, snake_list):
        for x in snake_list:
            pygame.draw.rect(dis, green, [x[0], x[1], snake_block, snake_block])
    
    
    def message(msg, color):
        mesg = font_style.render(msg, True, color)
        dis.blit(mesg, [dis_width / 6, dis_height / 3])
    
    
    def gameLoop():
        game_over = False
        game_close = False
    
        # 初始化贪吃蛇的位置和移动方向
        x1 = dis_width / 2
        y1 = dis_height / 2
        x1_change = 0
        y1_change = 0
    
        # 初始化贪吃蛇的身体长度
        snake_List = []
        Length_of_snake = 1
    
        # 随机生成食物的位置
        foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
        foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
    
        while not game_over:
    
            while game_close == True:
                dis.fill(blue)
                message("游戏结束! 按 Q 退出游戏,按 C 开始新游戏", red)
                pygame.display.update()
    
                for event in pygame.event.get():
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_q:
                            game_over = True
                            game_close = False
                        if event.key == pygame.K_c:
                            gameLoop()
    
            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:
                        x1_change = -snake_block
                        y1_change = 0
                    elif event.key == pygame.K_RIGHT:
                        x1_change = snake_block
                        y1_change = 0
                    elif event.key == pygame.K_UP:
                        y1_change = -snake_block
                        x1_change = 0
                    elif event.key == pygame.K_DOWN:
                        y1_change = snake_block
                        x1_change = 0
    
            if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
                game_close = True
            x1 += x1_change
            y1 += y1_change
            dis.fill(blue)
            pygame.draw.rect(dis, yellow, [foodx, foody, snake_block, snake_block])
            snake_Head = []
            snake_Head.append(x1)
            snake_Head.append(y1)
            snake_List.append(snake_Head)
            if len(snake_List) > Length_of_snake:
                del snake_List[0]
    
            for x in snake_List[:-1]:
                if x == snake_Head:
                    game_close = True
    
            our_snake(snake_block, snake_List)
            pygame.display.update()
    
            if x1 == foodx and y1 == foody:
                foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
                foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
                Length_of_snake += 1
    
            clock.tick(snake_speed)
    
        pygame.quit()
        quit()
    
    
    gameLoop()
    

    这是一个使用pygame库编写的简易贪吃蛇游戏。代码首先导入了pygame库,并初始化了游戏窗口和一些颜色。然后定义了屏幕的宽高、游戏速度、贪吃蛇的大小等参数。

    接下来定义了贪吃蛇的绘制函数our_snake和提示消息函数messageour_snake函数用于绘制贪吃蛇的身体,接受贪吃蛇的大小和身体列表作为参数,并使用pygame的绘图函数pygame.draw.rect来绘制贪吃蛇的每个身体部分。message函数用于在游戏结束时显示提示消息,接受消息内容和颜色作为参数,并使用pygame的字体函数pygame.font.SysFont和绘图函数dis.blit来绘制消息。

    接下来定义了游戏循环函数gameLoop。在游戏循环中,首先判断游戏是否结束,如果游戏结束则显示游戏结束的提示消息,并根据用户的输入来决定是退出游戏还是重新开始游戏。如果游戏没有结束,则根据用户的输入来改变贪吃蛇的位置和移动方向,并检查贪吃蛇是否撞墙或撞到自己,以及是否吃到食物。如果贪吃蛇吃到食物,则在随机位置生成新的食物,并增加贪吃蛇的长度。最后更新屏幕显示,并根据游戏速度设置时钟。

    最后调用gameLoop函数开始游戏循环。

    1年前 0条评论
注册PingCode 在线客服
站长微信
站长微信
电话联系

400-800-1024

工作日9:30-21:00在线

分享本页
返回顶部