代码

#-*-encoding=utf-8-*-
# Wormy(a Nibbles clone)
# By Al Sweigart al@inventwithpython.com
# http://inventwithpython.com/pygame
# Copied by Lei Song@1743207528@qq.com
import random,pygame,sys
from pygame.locals import * FPS = 15
WINDOWWIDTH = 640
WINDOWHEIGHT = 480
CELLSIZE = 20 # 指一个单独的格子的尺寸
assert WINDOWWIDTH%CELLSIZE == 0,'Window width must be a multiple of cell size.' # 断言,窗口宽度必须是一个格子尺寸的整数倍
assert WINDOWHEIGHT%CELLSIZE == 0,'Window height must be a multiple of cell size.' # 断言,窗口高度必须是一个格子尺寸的整数倍
CELLWIDTH = int(WINDOWWIDTH /CELLSIZE)
CELLHEIGHT = int (WINDOWHEIGHT/CELLSIZE) # R G B
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
DARKGREEN = (0,155,0)
DARKGRAY = (40,40,40)
BGCOLOR = BLACK UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right' HEAD = 0 # syntactic sugar:index of the worm's head 语法糖,头部为0 def main():
global FPSCLOCK,DISPLAYSURF,BASICFONT pygame.init() # 初始化
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
BASICFONT = pygame.font.Font('freesansbold.ttf',18)
pygame.display.set_caption('Wormy') showStartScreen()
while True:
runGame()
showGameOverScreen() def runGame():
# 确定一个随机的开始点
startx = random.randint(5,CELLWIDTH - 6)
starty = random.randint(5,CELLHEIGHT - 6)
wormCoords = [{'x':startx,'y':starty},
{'x':startx-1,'y':starty},
{'x':startx-2,'y':starty}]#存储了身体,初始长度为3
direction = RIGHT # 把苹果置于一个随机处
apple = getRandomLocation() while True:# main game loop
for event in pygame.event.get():# event handling loop
if event.type==QUIT:
terminate()
elif event.type == KEYDOWN:# 按下键盘,wasd或者箭头
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_ESCAPE:
terminate() # 检查它是否碰到自己或撞墙
if wormCoords[HEAD]['x']==-1 or wormCoords[HEAD]['x']==CELLWIDTH or wormCoords[HEAD]['y']==-1 or wormCoords[HEAD]['y']==CELLHEIGHT:
return# game over
for wormBody in wormCoords[1:]:
if wormBody['x']==wormCoords[HEAD]['x'] and wormBody['y']==wormCoords[HEAD]['y']:
return# game over # 检查它是否恰到苹果
if wormCoords[HEAD]['x']==apple['x'] and wormCoords[HEAD]['y']==apple['y']:
apple = getRandomLocation()
else:
del wormCoords[-1]# 删除尾部 # 如何做出移动的动画效果呢?方法是在蛇头前不断添加一个格子
if direction == UP:
newHead = {'x':wormCoords[HEAD]['x'],'y':wormCoords[HEAD]['y']-1}# 因为向下为正
elif direction == DOWN:
newHead = {'x':wormCoords[HEAD]['x'],'y':wormCoords[HEAD]['y']+1}
elif direction == LEFT:
newHead = {'x':wormCoords[HEAD]['x']-1,'y':wormCoords[HEAD]['y']}
elif direction == RIGHT:
newHead = {'x':wormCoords[HEAD]['x']+1,'y':wormCoords[HEAD]['y']} wormCoords.insert(0,newHead)# 加新头 # 绘制屏幕Drawing the Screen
DISPLAYSURF.fill(BGCOLOR)
drawGrid()
drawWorm(wormCoords)
drawApple(apple)
drawScore(len(wormCoords)-3)
pygame.display.update()# 这一句draws the display Surface to the actual computer screen
FPSCLOCK.tick(FPS/4) # 绘制“Press a key”文本到屏幕上
def drawPressKeyMsg():
pressKeySurf = BASICFONT.render('Press a key to play.',True,DARKGRAY)
pressKeyRect = pressKeySurf.get_rect()
pressKeyRect.topleft = (WINDOWWIDTH-200,WINDOWHEIGHT-30)
DISPLAYSURF.blit(pressKeySurf,pressKeyRect)
#这个函数将被showStartScreen()和showGameOverScreen()两个函数调用 def checkForKeyPress():
if len(pygame.event.get(QUIT))>0:# 返回一个列表,列表里是在事件队列中所有QUIT的事件,若无QUIT事件则返回空列表[]
terminate() keyUpEvents = pygame.event.get(KEYUP)
if len(keyUpEvents)==0:
return None
if keyUpEvents[0].key == K_ESCAPE:# 按了Esc键
terminate()
return keyUpEvents[0].key # 开始屏幕
def showStartScreen():
titleFont = pygame.font.Font('freesansbold.ttf',100)
titleSurf1 = titleFont.render('Wormy!',True,WHITE,DARKGREEN)# 白字,墨绿背景
titleSurf2 = titleFont.render('Wormy!',True,GREEN)# 绿字,透明背景 degrees1 = 0
degrees2 = 0
while True:
DISPLAYSURF.fill(BGCOLOR)
rotatedSurf1 = pygame.transform.rotate(titleSurf1,degrees1)# 不会改变titleSurf1,而是返回一个新的Surface,上面画着旋转后的图像
rotatedRect1 = rotatedSurf1.get_rect()
rotatedRect1.center = (WINDOWWIDTH/2,WINDOWHEIGHT/2)
DISPLAYSURF.blit(rotatedSurf1,rotatedRect1)
# 新的矩形往往比原先的大。旋转的角度逆时针为正,顺时针为负。pygame会把你传的大于等于360°的度数不断减去360°直到其小于360°
rotatedSurf2 = pygame.transform.rotate(titleSurf2,degrees2)
rotatedRect2 = rotatedSurf2.get_rect()
rotatedRect2.center = (WINDOWWIDTH/2,WINDOWHEIGHT/2)
DISPLAYSURF.blit(rotatedSurf2,rotatedRect2)
# 我们之所以不直接旋转rotatedSurf1和2是因为:1.旋转后的图形会失真,就像一张照片复印后再复印后不会与原来完全一样。不过如果旋转的角度为90°的倍数则不会出现这个问题。2.旋转后的矩形会变大,最终会超出pygame的承受范围
drawPressKeyMsg() if checkForKeyPress():
pygame.event.get()# 清空事件队列
return
pygame.display.update()
FPSCLOCK.tick(FPS)
degrees1 += 3# 每帧转3°
degrees2 += 7# 每帧转7° def terminate():
pygame.quit()
sys.exit() def getRandomLocation():
return {'x':random.randint(0,CELLWIDTH-1),'y':random.randint(0,CELLHEIGHT - 1)}
# 决定苹果出现的位置 def showGameOverScreen():
gameOverFont = pygame.font.Font('freesansbold.ttf',150)
gameSurf = gameOverFont.render('Game',True,WHITE)
overSurf = gameOverFont.render('Over',True,WHITE)
gameRect = gameSurf.get_rect()
overRect = overSurf.get_rect()
gameRect.midtop = (WINDOWWIDTH/2,10)
overRect.midtop = (WINDOWWIDTH/2,gameRect.height+10+25) DISPLAYSURF.blit(gameSurf,gameRect)
DISPLAYSURF.blit(overSurf,overRect)
drawPressKeyMsg()
pygame.display.update()
pygame.time.wait(500)# 500毫秒,也就是半秒
checkForKeyPress()# 清空事件队列中的任何按键 while True:
if checkForKeyPress():
pygame.event.get()# 清空事件队列
return def drawScore(score):
scoreSurf = BASICFONT.render('Score:%s'%(score),True,WHITE)
scoreRect = scoreSurf.get_rect()
scoreRect.topleft = (WINDOWWIDTH-120,10)
DISPLAYSURF.blit(scoreSurf,scoreRect) def drawWorm(wormCoords):
for coord in wormCoords:
x = coord['x']*CELLSIZE
y = coord['y']*CELLSIZE
wormSegmentRect = pygame.Rect(x,y,CELLSIZE,CELLSIZE)
pygame.draw.rect(DISPLAYSURF,DARKGREEN,wormSegmentRect)
wormInnerSegmentRect = pygame.Rect(x+4,y+4,CELLSIZE-8,CELLSIZE-8)
pygame.draw.rect(DISPLAYSURF,GREEN,wormInnerSegmentRect) def drawApple(coord):
x = coord['x']*CELLSIZE
y = coord['y']*CELLSIZE
appleRect = pygame.Rect(x,y,CELLSIZE,CELLSIZE)
pygame.draw.rect(DISPLAYSURF,RED,appleRect) def drawGrid():
for x in range(0,WINDOWWIDTH,CELLSIZE):# draw vertical lines
pygame.draw.line(DISPLAYSURF,DARKGRAY,(x,0),(x,WINDOWHEIGHT))
for y in range(0,WINDOWHEIGHT,CELLSIZE):# draw horizontal lines
pygame.draw.line(DISPLAYSURF,DARKGRAY,(0,y),(WINDOWWIDTH,y)) if __name__=='__main__':
main()

遇到的问题

在参考的源代码中,有下面这样一段(apple打错了,打成了apply):

            # check if worm has eaten an apply
if wormCoords[HEAD]['x'] == apple['x'] and wormCoords[HEAD]['y'] == apple['y']:
# don't remove worm's tail segment
apple = getRandomLocation() # set a new apple somewhere
else:
del wormCoords[-1] # remove worm's tail segment # move the worm by adding a segment in the direction it is moving
if direction == UP:
newHead = {'x': wormCoords[HEAD]['x'], 'y': wormCoords[HEAD]['y'] - 1}
elif direction == DOWN:
newHead = {'x': wormCoords[HEAD]['x'], 'y': wormCoords[HEAD]['y'] + 1}
elif direction == LEFT:
newHead = {'x': wormCoords[HEAD]['x'] - 1, 'y': wormCoords[HEAD]['y']}
elif direction == RIGHT:
newHead = {'x': wormCoords[HEAD]['x'] + 1, 'y': wormCoords[HEAD]['y']}
wormCoords.insert(0, newHead)

这段代码的第一个 if-else 意思是,如果蛇恰到了苹果,就重新生成一个苹果的位置,否则删去蛇尾的一个方块。下面的四句 if 和 elif 则是根据蛇运动的不同方向为蛇添加一个新的头部方块。

我一想,这不是多余了吗?为什么每次都要添加头部,在吃不到苹果的时候才删除尾部呢,直接在吃到苹果的时候添加头部不就简单了吗?于是下面是我一开始写的代码:

        # 检查它是否恰到苹果
if wormCoords[HEAD]['x']==apple['x'] and wormCoords[HEAD]['y']==apple['y']:
apple = getRandomLocation()
if direction == UP:
newHead = {'x':wormCoords[HEAD]['x'],'y':wormCoords[HEAD]['y']-1}# 因为向下为正
elif direction == DOWN:
newHead = {'x':wormCoords[HEAD]['x'],'y':wormCoords[HEAD]['y']+1}
elif direction == LEFT:
newHead = {'x':wormCoords[HEAD]['x']-1,'y':wormCoords[HEAD]['y']}
elif direction == RIGHT:
newHead = {'x':wormCoords[HEAD]['x']+1,'y':wormCoords[HEAD]['y']}
wormCoords.insert(0,newHead)# 加新头

没想到,运行后开始游戏后蛇完全不动弹!成了一张静止的图片!这是为什么呢?



于是我开始思考,那么蛇到底是怎么移动起来的呢?原来,答案就在上面这段源代码中。蛇“移动”的方式是不断向蛇头添加小块同时删除尾部小块,在吃到苹果时才不删除尾部,这样就能做出“移动”的效果啦。

参考

inventwithpython

Pygame 贪吃蛇的更多相关文章

  1. 初入pygame——贪吃蛇

    一.问题利用pygame进行游戏的编写,做一些简单的游戏比如贪吃蛇,连连看等,后期做完会把代码托管. 二.解决 1.环境配置 python提供一个pygame的库来进行游戏的编写.首先是安装pygam ...

  2. pygame编写贪吃蛇

    一直想用pygame做一个小游戏的,可是因为拖延症的缘故一直没有动,结果那天看到了一个12岁的国际友人小盆友用pygame做的一款塔防游戏,突然感觉已经落后超级远了,所以心血来潮做小游戏了.高中陪伴我 ...

  3. pygame写贪吃蛇

    python小白尝试写游戏.. 学了点pygame不知道那什么练手好,先拿贪吃蛇开刀吧. 一个游戏可以粗略的分为两个部分: 数据(变量) 处理数据(函数,方法) 设计变量 首先预想下,画面的那些部分需 ...

  4. 用python+pygame写贪吃蛇小游戏

    因为python语法简单好上手,前两天在想能不能用python写个小游戏出来,就上网搜了一下发现了pygame这个写2D游戏的库.了解了两天再参考了一些资料就开始写贪吃蛇这个小游戏. 毕竟最开始的练手 ...

  5. pygame试水,写一个贪吃蛇

    最近学完python基础知识,就想着做一个游戏玩玩,于是就在https://www.pygame.org/docs/学着做了个贪吃蛇游戏. 首先要导入模块. import pygame import ...

  6. Python实战练习_贪吃蛇 (pygame的初次使用)

    正如标题所写的那样,我将一步步的完成本次实战练习——贪吃蛇.废话不多说,感兴趣的伙伴可以一同挑战一下. 首先说明本次实战中我的配备: 开发环境:python 3.7: 开发工具:pycharm2019 ...

  7. Pygame模块实现功能超赞的贪吃蛇

    import pygame import random import sys import pygame.freetype import re import datetime   pygame.ini ...

  8. 程序游戏推荐(C语言贪吃蛇,python天天酷跑(需要安装pygame),js是狠人就坚持30s)

    下面是下载位置,我把他们上传到我的文件下了. C语言贪吃蛇:https://files.cnblogs.com/files/ITXiaoAng/%E8%B4%AA%E5%90%83%E8%9B%87. ...

  9. 多线程的Python 教程--“贪吃蛇”

    本指南的里代码可以在这里下载:  threadworms.py ,或者从  GitHub.代码需要  Python 3 或 Python 2 ,同时也需要安装  Pygame . 点击查看大版本图片 ...

随机推荐

  1. MySQL 统计上一周从周一到周日的用户

    这个功能按理说很常见,奇怪的是很难搜索到一个合适的.稍微整理了下,具体的就不展开了,注意这个表中的时间为毫秒,这条语句拷贝复制就能用.照顾大部分的无脑码农. SELECT case when FROM ...

  2. 提取线条的lines_color、lines_facet、 lines_gauss算子

    Halcon中线条提取的算子主要有: lines_color(Image : Lines : Sigma, Low, High, ExtractWidth, CompleteJunctions : ) ...

  3. 查看Windows系统进程(PID)

    语法:tasklist /svc 作用:打印系统进程,并显示其对应PID,可用来跟踪进程并根据PID来进行关闭.

  4. Vi 和 Vim 编辑器详细使用方法

    学习linux的一项必会技能,熟练使用vi/vim编辑器那便最重要的了.不过一堆操作看的也是太头疼了,以下整理了些常用到的命令. 工作模式 vi编辑界面有三种不同的工作模式,分别为命令模式.输入模式. ...

  5. Java使用JsonPatch

    老规矩,概念的东西不再此处体现,baidu即可自行解决,直入主题,动手第一. 导入所需的jar文件 pom.xml     <dependencies>        <depend ...

  6. openssl获取ssl证书,配置https

  7. Windows 将FTP 映射到本地文件夹 --简化操作

    转载自yutiantongbu Windows 将FTP 映射到本地文件夹 --简化操作 1.右键我的电脑,选择映射网络驱动器 2.选择"连接到可用与存储文档和图片的网站" 3.接 ...

  8. Java7与Java8中的HashMap和ConcurrentHashMap知识点总结

    JAVA7 Java7的ConcurrentHashMap里有多把锁,每一把锁用于其中一部分数据,那么当多线程访问容器里不同数据段的数据时,线程间就不会存在锁竞争,从而可以有效的提高并发访问效率呢.这 ...

  9. 项目Alpha冲刺 7

    作业描述 课程: 软件工程1916|W(福州大学) 作业要求: 项目Alpha冲刺(团队) 团队名称: 火鸡堂 作业目标: 介绍第7天冲刺的项目进展.问题困难和心得体会 1.团队信息 队名:火鸡堂 队 ...

  10. PHP程序员最容易犯的Mysql错误

    对于大多数web应用来说,数据库都是一个十分基础性的部分.如果你在使用PHP,那么你很可能也在使用MySQL—LAMP系列中举足轻重的一份子. 对于很多新手们来说,使用PHP可以在短短几个小时之内轻松 ...