找回密码
 中文实名注册
查看: 845|回复: 6

冯泽代--贪吃蛇

[复制链接]

4

主题

85

帖子

3685

积分

论坛元老

Rank: 8Rank: 8

积分
3685
发表于 2023-3-19 20:03:55 | 显示全部楼层 |阅读模式

[Python] 纯文本查看 复制代码
import pygame
import sys
import time
import random

pygame.init()
font_style = pygame.font.Font("C:/Windows/Fonts/msyhbd.ttc", 20)
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 = pygame.display.set_mode((800, 600))
pygame.display.set_caption('贪吃蛇')

clock = pygame.time.Clock()

snake_block = 10
snake_speed = 15

font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)


def Your_score(score):
    value = score_font.render("Score: " + str(score), True, yellow)
    dis.blit(value, [0, 0])


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, [800 / 6, 600 / 3])


def gameLoop():
    game_over = False
    game_close = False

    x1 = 800 / 2
    y1 = 600 / 2

    x1_change = 0
    y1_change = 0

    snake_List = []
    Length_of_snake = 1

    foodx = round(random.randrange(0, 800 - snake_block) / 10.0) * 10.0
    foody = round(random.randrange(0, 600 - snake_block) / 10.0) * 10.0

    while not game_over:

        while game_close == True:
            dis.fill(white)
            message("Please try again(click 'P')", green)
            Your_score(Length_of_snake - 1)
            pygame.display.update()
            for event in pygame.event.get():
                if(event.type == pygame.QUIT):
                    pygame.quit()
                    sys.exit(1)

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_p:
                        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 >= 800 or x1 < 0 or y1 >= 600 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)
        Your_score(Length_of_snake - 1)

        pygame.display.update()

        if x1 == foodx and y1 == foody:
            foodx = round(random.randrange(0, 800 - snake_block) / 10.0) * 10.0
            foody = round(random.randrange(0, 600 - snake_block) / 10.0) * 10.0
            Length_of_snake += 1

        clock.tick(snake_speed)

    pygame.quit()
    quit()


gameLoop()
while True:
   for event in pygame.event.get():
      if(event.type == QUIT):
        pygame.quit()
        sys.exit(1)




回复

使用道具 举报

4

主题

85

帖子

3685

积分

论坛元老

Rank: 8Rank: 8

积分
3685
 楼主| 发表于 2023-3-26 21:56:45 | 显示全部楼层
[C++] 纯文本查看 复制代码
import pygame,sys
import time
import random
import pgzrun

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 = pygame.display.set_mode((800, 600))
pygame.display.set_caption('贪吃蛇--by冯泽代')

clock = pygame.time.Clock()

snake_block = 10
snake_speed = 15

font_style = pygame.font.Font("C:\Windows\Fonts\simsunb.ttf", 20)
score_font = pygame.font.Font("C:\Windows\Fonts\simsunb.ttf", 30)

def Your_score(score):
    value = score_font.render("Your Score: " + str(score), True, red)
    dis.blit(value, [0, 0])


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, [800 / 6, 600 / 3])


def gameLoop():
    game_over = False
    game_close = False

    x1 = 800 / 2
    y1 = 600 / 2

    x1_change = 0
    y1_change = 0

    snake_List = []
    Length_of_snake = 1

    foodx = round(random.randrange(0, 800 - snake_block) / 10.0) * 10.0
    foody = round(random.randrange(0, 600 - snake_block) / 10.0) * 10.0

    pygame.mixer.music.load("贪吃蛇.wav")
    pygame.mixer.music.play()

    while not game_over:


        while game_close == True:
            dis.fill(white)
            message("Please try again(click 'P')", green)
            Your_score(Length_of_snake - 1)
            pygame.display.update()
            for event in pygame.event.get():
                  if(event.type == pygame.QUIT):
                        pygame.quit()
                        sys.exit(1)            

                  if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_p:
                              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 >= 800 or x1 < 0 or y1 >= 600 or y1 < 0:
            game_close = True
        x1 += x1_change
        y1 += y1_change
        dis.fill(white)
        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)
        Your_score(Length_of_snake - 1)

        pygame.display.update()

        if x1 == foodx and y1 == foody:
            foodx = round(random.randrange(0, 800 - snake_block) / 10.0) * 10.0
            foody = round(random.randrange(0, 600 - snake_block) / 10.0) * 10.0
            Length_of_snake += 1

        clock.tick(snake_speed)

    pygame.quit()
    quit()



gameLoop()
while True:
   for event in pygame.event.get():
      if(event.type == QUIT):
         pygame.quit()
         sys.exit(1)
回复

使用道具 举报

4

主题

85

帖子

3685

积分

论坛元老

Rank: 8Rank: 8

积分
3685
 楼主| 发表于 2023-3-26 22:47:22 | 显示全部楼层
回复

使用道具 举报

4

主题

85

帖子

3685

积分

论坛元老

Rank: 8Rank: 8

积分
3685
 楼主| 发表于 2023-3-26 22:48:19 | 显示全部楼层
制作软件及运行环境:
海龟编辑器
Windows 10
回复

使用道具 举报

4

主题

85

帖子

3685

积分

论坛元老

Rank: 8Rank: 8

积分
3685
 楼主| 发表于 2023-3-26 22:52:03 | 显示全部楼层
创作过程:
运用了python中的两个库——pygame和pygame zero,这两个库都是用来做游戏的,并且游戏相对于普通而又枯燥无味的程序,拥有着相对高的自由性以及互动性。
回复

使用道具 举报

4

主题

85

帖子

3685

积分

论坛元老

Rank: 8Rank: 8

积分
3685
 楼主| 发表于 2023-3-26 22:55:09 | 显示全部楼层
创作思想:
我写这个程序是为了完成我小时候的一个梦想——制作一个能玩的游戏,并且也是为了怀旧,因为我们小时候都玩过贪吃蛇。
回复

使用道具 举报

4

主题

85

帖子

3685

积分

论坛元老

Rank: 8Rank: 8

积分
3685
 楼主| 发表于 2023-3-26 22:59:07 | 显示全部楼层
本帖最后由 冯泽代 于 2023-3-26 23:03 编辑

原创部分:
pygame.mixer.music.load("贪吃蛇.wav")
pygame.mixer.music.play()
-------------------------------------
while True:
   for event in pygame.event.get():
      if(event.type == QUIT):
         pygame.quit()
         sys.exit(1)
-------------------------------------
while game_close == True:
            dis.fill(white)
            message("Please try again(click 'P')", green)
            Your_score(Length_of_snake - 1)
            pygame.display.update()
            for event in pygame.event.get():
                if(event.type == pygame.QUIT):
                    pygame.quit()
                    sys.exit(1)

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_p:
                        gameLoop()
-------------------------------------
font_style = pygame.font.Font("C:\Windows\Fonts\simsunb.ttf", 20)
score_font = pygame.font.Font("C:\Windows\Fonts\simsunb.ttf", 30)
-------------------------------------
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




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

小黑屋|东台市机器人学会 ( 苏ICP备2021035350号-1;苏ICP备2021035350号-2;苏ICP备2021035350号-3 )

GMT+8, 2024-11-21 14:26 , Processed in 0.040053 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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