冯泽代 发表于 2023-3-19 20:03:55

冯泽代--贪吃蛇


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, )


def our_snake(snake_block, snake_list):
    for x in snake_list:
      pygame.draw.rect(dis, black, , x, snake_block, snake_block])


def message(msg, color):
    mesg = font_style.render(msg, True, color)
    dis.blit(mesg, )


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, )
      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

      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)




冯泽代 发表于 2023-3-26 21:56:45

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, )


def our_snake(snake_block, snake_list):
    for x in snake_list:
      pygame.draw.rect(dis, black, , x, snake_block, snake_block])


def message(msg, color):
    mesg = font_style.render(msg, True, color)
    dis.blit(mesg, )


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, )
      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

      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)

冯泽代 发表于 2023-3-26 22:47:22

参考资源:
http://c.biancheng.net/pygame/
https://geek-docs.com/pygame/pygame-tutorials/getting-started-with-pygame.html
https://cloud.tencent.com/developer/article/1888264
https://blog.csdn.net/qq_48174666/article/details/113253066
https://zhuanlan.zhihu.com/p/342609903
https://s1.aigei.com/src/aud/wav/ce/ce02ca06fa1743eabb860ebd47b1e30c.wav?download/%E8%B4%AA%E5%90%83%E8%9B%87%E5%A4%A7%E4%BD%9C%E6%88%98%E8%83%8C%E6%99%AF%E9%9F%B3%E4%B9%90_%E7%88%B1%E7%BB%99%E7%BD%91_aigei_com.wav&e=1679874420&token=P7S2Xpzfz11vAkASLTkfHN7Fw-oOZBecqeJaxypL:kbCYFBuxBjfZI8kTwWbVuOUiLrE=(音乐资源)

冯泽代 发表于 2023-3-26 22:48:19

制作软件及运行环境:
海龟编辑器
Windows 10

冯泽代 发表于 2023-3-26 22:52:03

创作过程:
运用了python中的两个库——pygame和pygame zero,这两个库都是用来做游戏的,并且游戏相对于普通而又枯燥无味的程序,拥有着相对高的自由性以及互动性。

冯泽代 发表于 2023-3-26 22:55:09

创作思想:
我写这个程序是为了完成我小时候的一个梦想——制作一个能玩的游戏,并且也是为了怀旧,因为我们小时候都玩过贪吃蛇。

冯泽代 发表于 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




页: [1]
查看完整版本: 冯泽代--贪吃蛇