gascd 发表于 2020-2-22 14:50:15

【python作业】贪吃蛇-pygame

本帖最后由 gascd 于 2020-2-22 14:51 编辑

github项目地址

import sys
import pygame
import random

WindowSize = ( 720, 480 )
fontSize = 20
cellSize = 16
mpSize = int(WindowSize/cellSize), int(WindowSize/cellSize)
#蛇的速度
snakeSpeed = 10

#颜色定义
colorBlack = ( 0, 0, 0)
colorRed = ( 255, 0, 0)
colorGreen = ( 0, 255, 0)
colorBlue = ( 0, 0, 255)
colorDarkBlue = ( 0, 0, 120)

#方向定义
left = 0
down = 1
up = 2
right = 3

#入口函数
def main():
    pygame.init()
    screen = pygame.display.set_mode(WindowSize)
    pygame.display.set_caption("贪吃蛇小游戏~~")
    snakeClock = pygame.time.Clock()
    imgGameStart = pygame.image.load('./img/gameStart.jpg')

    while 1:
      showGameStartInfo(screen, imgGameStart)
      gameRunning(screen, snakeClock)
      
#展示游戏开始信息
def showGameStartInfo(screen, imgGameStart):
    font = pygame.font.Font("./font/msyh.ttc", fontSize)
    screen.blit(imgGameStart, (0,0))
    fontBuffer = font.render("按任意键开始游戏", True, colorRed)
    screen.blit(fontBuffer, (250, 300))
    fontBuffer = font.render("按q或ESC结束游戏", True, colorRed)
    screen.blit(fontBuffer, (250, 300 + fontSize))      
    pygame.display.update()

    while True:
      for event in pygame.event.get():
            justiceGameOver(event)
            if event.type == pygame.KEYDOWN:
                return
      
#退出游戏
def terminate():
    pygame.quit()
    sys.exit()   

#展示游戏结束信息
def showGameOverInfo(screen, imgGameEnd, score):
    font = pygame.font.Font("./font/msyh.ttc", fontSize)
    screen.blit(imgGameEnd, (0,0))
    fontBuffer = font.render("你的得分是%s" % score, True, colorRed)
    screen.blit(fontBuffer, (250, 300 - fontSize))
    fontBuffer = font.render("按r重新开始游戏", True, colorRed)
    screen.blit(fontBuffer, (250, 300))
    fontBuffer = font.render("按q或ESC结束游戏", True, colorRed)
    screen.blit(fontBuffer, (250, 300 + fontSize))      
    pygame.display.update()
    while True:
      for event in pygame.event.get():
            justiceGameOver(event)
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:
                  return

#游戏运行主体
def gameRunning(screen, snakeClock):
    startxy = -5), random.randint(5, mpSize-5)]
    snake = [
      {'x':startxy, 'y':startxy},
      {'x':startxy-1, 'y':startxy},
      {'x':startxy-2, 'y':startxy}
    ]
    direction = right
    score = 0
    food = CreateFood(screen)

    while True:
      if food not in snake:
            del snake[-1]
      else:
            score += 1
            food = CreateFood(screen)
      for event in pygame.event.get():
            justiceGameOver(event)
            direction = justiceSnakeTurn(event, direction)
      snakeMove(direction, snake)
      if not isSnakeAlive(snake):
            imgGameEnd = pygame.image.load('./img/gameEnd.jpg')
            showGameOverInfo(screen, imgGameEnd, score)
            return
      screen.fill(colorBlack)
      DrawFood(screen, food)
      DrawSnake(screen, snake)
      DrawScore(screen, score)
      pygame.display.update()
      
      snakeClock.tick(snakeSpeed)

#判断蛇是否活着 超出边界 吃到自己就是死
def isSnakeAlive(snake):
    if snake['x'] == -1 or snake['x'] == mpSize or snake['y'] == -1 or snake['y'] == mpSize:
      return False
    elif snake in snake:
      return False
    return True

#蛇移动
def snakeMove(direction, snake):
    if direction == left:
      tmp = {'x':snake['x']-1, 'y':snake['y']}
    elif direction == down:
      tmp = {'x':snake['x'], 'y':snake['y']+1}
    elif direction == up:
      tmp = {'x':snake['x'], 'y':snake['y']-1}
    elif direction == right:
      tmp = {'x':snake['x']+1, 'y':snake['y']}

    snake = snake.insert(0, tmp)
#画出蛇
def DrawSnake(screen, snake):
    for i in snake:
      x = i['x'] * cellSize
      y = i['y'] * cellSize
      tmp = (x, y, cellSize, cellSize)
      pygame.draw.rect(screen, colorDarkBlue, tmp)
      tmp = (x+3, y+3, cellSize-6, cellSize-6)
      pygame.draw.rect(screen, colorBlue, tmp)


#创造食物
def CreateFood(screen):
    xy = {'x':random.randint(0, mpSize-1), 'y':random.randint(0, mpSize-1)}
    return xy
#画出食物
def DrawFood(screen, xy):
    x = xy['x'] * cellSize
    y = xy['y'] * cellSize
    appleRect = (x, y, cellSize, cellSize)
    pygame.draw.rect(screen, colorGreen, appleRect)
#画出分数
def DrawScore(screen, score):
    font = pygame.font.Font("./font/msyh.ttc", fontSize)
    fontBuffer = font.render("得分:%s" % score, True, colorRed)
    screen.blit(fontBuffer, (600 ,0))

#判断蛇转弯的方向
def justiceSnakeTurn(event, direction):
    if event.type == pygame.KEYDOWN:
      if event.key in (pygame.K_LEFT, pygame.K_a) and direction != right:
            direction = left
      elif event.key in(pygame.K_RIGHT, pygame.K_d) and direction != left:
            direction = right
      elif event.key in (pygame.K_UP, pygame.K_w) and direction != down:
            direction = up
      elif event.key in (pygame.K_DOWN, pygame.K_s) and direction != up:
            direction = down

    return direction

#判断游戏是否应该结束
def justiceGameOver(event):
    if event.type == pygame.QUIT:
      terminate()
    elif event.type == pygame.KEYDOWN:
      if event.key == pygame.K_q or event.key == pygame.K_ESCAPE:
            terminate()
      

main()

Sungne_jer 发表于 2020-2-22 15:00:13

厉害

乘号 发表于 2020-4-13 10:17:41

这么好的贴竟然沉了!

小琳1011 发表于 2020-4-13 15:44:33

17851305596 发表于 2020-4-14 00:46:46

F:\Python\python.exe "F:/Program Files/untitled/3.py"
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "F:/Program Files/untitled/3.py", line 195, in <module>
    main()
File "F:/Program Files/untitled/3.py", line 32, in main
    imgGameStart = pygame.image.load('./img/gameStart.jpg')
pygame.error: Couldn't open ./img/gameStart.jpg
这是什么错误啊

gascd 发表于 2020-4-19 17:29:05

17851305596 发表于 2020-4-14 00:46
F:\Python\python.exe "F:/Program Files/untitled/3.py"
pygame 1.9.6
Hello from the pygame community ...

这个是无法打开那个图片。
因为我没有写异常处理,所以他会直接报错。
你去github里吧源码整个都下载下就可以了。
页: [1]
查看完整版本: 【python作业】贪吃蛇-pygame