资源简介
贪吃蛇大作战,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
相关资源
- Python3学习笔记
- Python数据挖掘和实战课程源码
- Python编程快速上手 让繁琐工作自动化
- 卷积神经网络python
- Python3.7.2中文文档-标准库-通用操作系
- Python3.7.2中文文档-标准库-Python数据类
- pygame-1.9.6-cp36-cp36m-win_amd64.whl
- Introduction to Data Science - A Python Approa
- python网络爬虫爬取整个网页
- Deep Learning With Python - Jason Brownlee
- Long Short Terms Memory Networks with Python J
- 破解wifi密码python小程序
- python之外星人入侵完整版程序源码
- python之外星人入侵
- 基于python的小游戏 含源代码
- 深度学习入门 基于python理论与实现
- 混合地理加权回归python实现代码
- 基于python的单机版中国象棋
- python3基础教程第三版高清
- python入门笔记强烈推荐.zip
- Python-自然场景文本检测PSENet的一个
- Python-在特征金字塔网络FPN的Pytorch实现
- Python-PyTorch实时多人姿态估计项目的实
- Python-用PyTorch10实现FasterRCNN和MaskRCNN比
- Python-心脏核磁共振MRI图像分割
- Python-基于YOLOv3的行人检测
- Python-数据结构与算法leetcodelintcode题解
- Python-RLSeq2Seq用于SequencetoSequence模型的
- Python-PyTorch对卷积CRF的参考实现
- Python-高效准确的EAST文本检测器的一个
评论
共有 条评论