Python贪吃蛇小游戏

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_33189961/article/details/98176379

为了复习一下这几天的学习内容,照猫画虎来个小游戏

预览成果

导入所需的库

import pygame    #游戏库
import random    #随机数,用来生成蛇点
import sys       #界面操
from pygame.locals import *   #导入pygame所需的常量

定义所需的颜色

redColor = pygame.Color(255,0,0)
blackColor = pygame.Color(0,0,0)  #黑色,背景颜色
whiteColor = pygame.Color(255,255,255)  #白色,贪吃蛇的颜色
def gameOver():
    pygame.quit()
    sys.exit()

定义main函数

一些准备工作
pygame.init()  #初始化pygame
    fpsClock = pygame.time.Clock()  #定义游戏速度
    playSurface = pygame.display.set_mode((640,480))   #创建游戏界面
    pygame.display.set_caption("贪吃蛇")   #创建窗口标题
    snakePosition = [100,100]   #贪吃蛇起始位置
    snakeBody = [[100,100],[100,100],[100,100]]#初始化蛇的长度,用列表的元素长度代替蛇身位置
    targetPosition = [300,300]  #初始化第一个方块位置
    targetFlag = 1   #目标方块标记,1:没吃掉   0:吃掉了
    direction = "right"   #初始化方向 ,向右
    changeDirection = direction #方向变量,人为控制
获取键盘事件
 while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:  #键盘按下
                if event.key == K_RIGHT:
                    changeDirection = "right"
                if event.key == K_LEFT:
                    changeDirection = "left"
                if event.key == K_UP:
                    changeDirection = "up"
                if event.key == K_DOWN:
                    changeDirection = "down"
确定方向,不允许直接改为反方向
        if changeDirection == "left" and not direction =="right":
            direction = changeDirection
        if changeDirection == "right" and not direction =="left":
            direction = changeDirection
        if changeDirection == "up" and not direction =="down":
            direction = changeDirection
        if changeDirection == "down" and not direction =="up":
            direction = changeDirection
根据方向移动蛇头
        if direction == "right":
            snakePosition[0] += 20
        if direction == "left":
            snakePosition[0] -= 20
        if direction == "up":
            snakePosition[1] -= 20
        if direction == "down":
            snakePosition[1] += 20
         """增加蛇的长度"""
        snakePosition.insert(0,list(snakePosition))

吃掉小方块

  """位置重合即为吃掉"""
        if snakePosition[0] == targetPosition[0] and snakePosition[1] == targetPosition[1]:
            targetFlag = 0
        else:
            snakeBody.pop()

限制小点生成位置

  if targetFlag == 0:
            #防止点生到界面外面
            x = random.randrange(1,32)
            y = random.randrange(1,24)
            targetPosition = [int(x*20),int(y*20)]
            targetFlag = 1

填充背景颜色

 playSurface.fill(blackColor) 

绘制角色

        for position in snakeBody:
            pygame.draw.rect(playSurface,whiteColor,Rect(position[0],position[1],20,20))  #绘制蛇
            #绘制点
            pygame.draw.rect(playSurface, redColor, Rect(targetPosition[0], targetPosition[1], 20, 20))
            pygame.display.flip()    #显示到屏幕

撞墙则死掉

  if snakePosition[0] > 620 or snakePosition[0] < 0:
     gameOver()
  elif snakePosition[1] > 460 or snakePosition[1] < 0:
     gameOver()

控制游戏速度

 fpsClock.tick(5)

启动函数

if __name__ == "__main__":
    main()

 

 

 

 

 

 

展开阅读全文

没有更多推荐了,返回首页