python简单贪吃蛇开发

时间:2022-09-13 07:51:59

本文实例为大家分享了python简单贪吃蛇的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import sys
import random
import pygame
from pygame.locals import *
 
# 目标方块的颜色 红色
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函数
def main():
  # 初始化pygame
  pygame.init()
  # 定义一个控制速度的函数
  fpsClock = pygame.time.Clock()
  # 创建显示层
  playSurface = pygame.display.set_mode((640,480))  # 界面的大小
  pygame.display.set_caption('贪吃蛇')
  # 初始化蛇的位置
  snake_position=[100,100]
 
  # 初始化蛇的长度
  snake_body = [[100,100],[80,100],[60,100]]
 
  # 初始化目标方块的位置
  target_position = [300,300]
 
  # 目标方块的状态
  target_flag = 1
 
  # 初始化一个方向
  direction = 'right'
  # 定义蛇的方向变量
  changeDirection = direction
 
  while True:
    # pygame的交互模块和事件队列
    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 event.key == K_SPACE:
          pygame.event.post(pygame.event.Event(QUIT))
 
    # 根据键盘反应确定方向
    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':
      snake_position[0] += 20
 
    if direction == 'left':
      snake_position[0] -= 20
 
    if direction == 'up':
      snake_position[1] -= 20
 
    if direction == 'down':
      snake_position[1] += 20
 
    # 蛇与自身的碰撞检测
    for body in snake_body:
      if snake_position[0] == body[0] and snake_position[1] == body[1]:
        gameOver()
 
    # 蛇移动
    snake_body.insert(0,list(snake_position))
 
 
 
    if snake_position[0] == target_position[0] and snake_position[1] == target_position[1]:
      target_flag = 0
    else:
      # 如果没吃到,蛇尾弹出栈
      snake_body.pop()
 
    # 如果吃掉目标方块,重新生成一个目标方块
    if target_flag == 0:
      x = random.randrange(1,32)
      y = random.randrange(1,24)
      # 20*20的像素为一个小矩形
      target_position = [int(x*20),int(y*20)]
      target_flag = 1
 
    # 绘制显示层
    playSurface.fill(blackColor)
 
    # 绘制蛇
    for position in snake_body:
      pygame.draw.rect(playSurface, redColor, Rect(position[0],position[1],20,20))
 
    # 画目标方块
    pygame.draw.rect(playSurface, whiteColor, Rect(target_position[0], target_position[1], 20, 20))
 
    pygame.display.flip()
 
    # 判断死亡
    if snake_position[0] > 620 or snake_position[1] < 0:
      gameOver()
    elif snake_position[1] > 460 or snake_position[1] < 0:
      gameOver()
 
    # 控制游戏的速度
    fpsClock.tick(5)
 
if __name__ == '__main__':
  main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/wwxy1995/article/details/79998066