资源简介
贪吃蛇大作战,Python Pygame小游戏开发入门,包含多目标,背景音乐,中文显示,多彩颜色控制等,代码注释清晰易懂,适合入门级,增加学习乐趣。

代码片段和文件信息
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pygame sys random
from pygame.locals import *
blueColor = pygame.Color(0 0 255)
whiteColor = pygame.Color(255 255 255)
redColor = pygame.Color(255 0 0)
blackColor = pygame.Color(0 0 0)
orangeColor = pygame.Color(255 165 0)
sound = r‘D:\01\Python\test\test\7895.wav‘
def ko():
pygame.quit()
sys.exit()
def main():
pygame.init()
pygame.mixer.init()
pygame.time.delay(1000)
pygame.mixer.music.load(sound)
pygame.mixer.music.play(-1)
fpsClock = pygame.time.Clock()
score = 0
screen = pygame.display.set_mode((640 480))
pygame.display.set_caption(‘贪吃蛇大作战+背景音乐+多目标 Charlie工作室出品‘)
#初始化贪吃蛇初始化位置
snakePos = [100 100]
#初始化贪吃蛇长度列表,有几段代表几段身体
snakeBody = [[100 100] [80 100] [60 100]]
#初始化目标方块位置
target = [300 300]
target1= [400 400]
target2 = [200 200]
#目标方块的标记:判断是否吃掉了这个方块 1代表没有吃掉,0代表吃掉
flag = 1
flag1 = 1
flag2 = 1
#初始化方向
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_d or event.key == K_RIGHT:
changedirection = ‘right‘
if event.key == K_a or event.key == K_LEFT:
changedirection = ‘left‘
if event.key == K_w or event.key == K_UP:
changedirection = ‘up‘
if event.key == K_s or event.key == K_DOWN:
changedirection = ‘down‘
if event.key == K_ESCAPE:
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‘:
snakePos[0] += 20
if direction == ‘left‘:
snakePos[0] -= 20
if direction == ‘up‘:
snakePos[1] -= 20
if direction == ‘down‘:
snakePos[1] += 20
#增加蛇身长度
snakeBody.insert(0 list(snakePos))
#如果贪吃蛇位置与目标方块重合
if snakePos[0] == target[0] and snakePos[1] == target[1]:
flag = 0
score = score+10
elif snakePos[0] == target1[0] and snakePos[1] == target1[1]:
flag1 = 0
score = score+10
elif snakePos[0] == target2[0] and snakePos[1] == target2[1]:
flag2 = 0
score = s
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-10-10 10:15 Snake\
文件 4360044 2018-10-08 12:50 Snake\7895.wav
文件 4135804 2000-01-10 12:00 Snake\GB2312.ttf
文件 5074 2018-10-10 10:13 Snake\SnakeOne.py
相关资源
- Python-BDD100K大规模多样化驾驶视频数据
- Instant Pygame for Python Game Development How
- Biopython Tutorial
- Think Python 2nd
- 一个小小的表白程序(python)
- Python课堂笔记(高淇400集第一季)
- 二级考试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管控
评论
共有 条评论