资源简介
最近又学了一下python,听说pygame挺好玩的,研究了好久, 弄了个贪吃蛇出来,在一位网友的基础上加了不少东西,加减速,暂停,排名,障碍物弄了一半。希望没女朋友的大家喜欢~~~

代码片段和文件信息
## 导入相关模块
import random
import pygame
import sys
from pygame.locals import *
windows_width = 800
windows_height = 600 # 游戏窗口的大小
cell_size = 20 # 贪吃蛇身体方块大小注意身体大小必须能被窗口长宽整除
‘‘‘ #初始化区
由于我们的贪吃蛇是有大小尺寸的 因此地图的实际尺寸是相对于贪吃蛇的大小尺寸而言的
‘‘‘
map_width = int(windows_width / cell_size)
map_height = int(windows_height / cell_size)
# 颜色定义
white = (255 255 255)
black = (0 0 0)
gray = (230 230 230)
dark_gray = (40 40 40)
DARKGreen = (0 155 0)
Green = (0 255 0)
Red = (255 0 0)
blue = (0 0 255)
dark_blue = (0 0 139)
FOOD_color = [(10 (255 100 100)) (20 (100 255 100)) (30 (100 100 255))] # 食物颜色
BG_COLOR = black # 游戏背景颜色
# 定义方向
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4
HEAD = 0 # 贪吃蛇头部下标
score_add = []
def running_game(screen snake_speed_clocknum):
change_speed = 0 # 定义改变速度
start_speed = 2 # 贪吃蛇的原始速度+2
startx = random.randint(20 map_width - 20) # 开始位置
starty = random.randint(10 map_height - 10)
snake_coords = [{‘x‘: startx ‘y‘: starty} # 初始贪吃蛇
{‘x‘: startx - 1 ‘y‘: starty}
{‘x‘: startx - 2 ‘y‘: starty}]
direction = RIGHT # 开始时向右移动
food = get_random_location() # 实物随机位置
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
elif event.type == KEYDOWN: # and 判断是否输入了反方向
if (event.key == K_LEFT or event.key == K_a) and direction != RIGHT:
direction = LEFT
elif (event.key == K_RIGHT or event.key == K_d) and direction != LEFT:
direction = RIGHT
elif (event.key == K_UP or event.key == K_w) and direction != DOWN:
direction = UP
elif (event.key == K_DOWN or event.key == K_s) and direction != UP:
direction = DOWN
elif event.key == K_SPACE:
pause(screen)
elif event.key == K_ESCAPE:
terminate()
elif event.key == K_1: # 按“1”加速一局加速不超过10
if change_speed < 10:
change_speed += 1
elif event.key == K_2: # 按“2”减速,减速次数不能超过加速5次
if change_speed > -5 and len(snake_coords) > 8:
change_speed -= 1
move_snake(direction snake_coords) # 移动蛇
ret = snake_is_alive(snake_coords)
if not ret:
break # 蛇跪了. 游戏结束
snake_is_eat_food(snake_coords food) # 判断蛇是否吃到食物
screen.fill(BG_COLOR)
draw_grid(screen)
draw_snake(screen snake_coords)
draw_food(screen food)
draw_score(screen len(snake_coords) - 3)
pygame.display.update()
# 控制速度
if len(snake_coords) < 40:
snake_speed = change_speed + len(snake_coords) // 2 + start_speed
draw_speed(screen change_speed + len(snake_coords) // 2 + start_speed)
pygame.display.update()
else:
snake_speed = 10 # 长度到达40时,若一直按减速,则看到速度最小为10
draw_speed(screen 10)
pygame.display.update()
snake_speed_clock.tick(snake_speed)
keep_rangking(len(snake_coords)-4 num)
show_gameover_info(screenlen(snake_coords)-4num)
# 将食物画出来
def draw_food(screen food):
x = food[‘x‘] * cell_size
y = food[‘y‘] * cell_size
appleRect = pygame.Rect(x y cell_size
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-12-12 10:12 snake\
文件 1195 2018-12-11 20:08 snake\h.jpg
文件 19347 2018-12-11 19:56 snake\l.png
文件 4381372 2017-12-19 16:41 snake\myfont.ttf
文件 192520 2018-12-10 17:10 snake\over.png
文件 87281 2018-12-11 20:25 snake\rangking.jpg
文件 10121 2018-12-12 10:12 snake\snake.py
文件 67359 2018-12-10 19:16 snake\start.png
文件 1237 2018-12-11 20:13 snake\timg.jpg
相关资源
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
评论
共有 条评论