Python实现贪吃蛇小游戏(单人模式)_第1页
Python实现贪吃蛇小游戏(单人模式)_第2页
Python实现贪吃蛇小游戏(单人模式)_第3页
Python实现贪吃蛇小游戏(单人模式)_第4页
Python实现贪吃蛇小游戏(单人模式)_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第Python实现贪吃蛇小游戏(单人模式)简单用py写了一个贪吃蛇游戏,有单人、双人模式,比较简单,适合初学者练手。基本上每行重要的语句都有注释,做了什么事一目了然

这里先介绍单人模式

一、游戏设计要点

1.游戏主体窗口(尺寸)、画布(尺寸、位置)、按钮(尺寸、位置)、文字(大小、颜色、位置)、图像、背景音乐及相关响应函数(主要是鼠标移动及点击的响应)的设计与合理排布

2.蛇与食物的类的属性设计

3.蛇位置的更新(根据键盘输入)、吃到食物加分的判定、食物的更新

4.蛇死亡的判定条件设计

二、主要模块

1.pygame

2.sys

3.random

三、用到的类

1.Snake类,定义蛇头蛇身元素的位置

2.Food类,定义食物元素的位置及单个元素的颜色

四、主要函数

1.new_food(),功能:生成一个不与蛇头位置重合的食物并返回该食物对象

defnew_food(head):

whileTrue:

#循环,不断实例化new_food对象直到生成一个不与蛇头重合的食物

new_food=Food(randint(0,45)*20,randint(0,28)*20,(randint(10,255),randint(10,255),randint(10,255)))

#若new_food和蛇头重合则不创键

ifnew_food.x!=head.xandnew_food.y!=head.y:

break

else:

continue

returnnew_food

2.draw_snake()、draw_food()函数,功能:绘制蛇与食物的图像,传入参数为颜色和对象:

#在窗体中绘制贪吃蛇

#形参:一个是颜色另一个是实例化对象

defdraw_snake(color,object):

pygame.draw.circle(window,color,(object.x,object.y),10)

#在窗体中绘制食物

#形参:实例化对象

defdraw_food(food):

pygame.draw.circle(window,food.color,(food.x,food.y),10)

3.show_end函数,功能:显示单人模式结束时的得分界面:

#游戏结束时,单人模式显示得分的窗体的设置

defshow_end_single():

whileTrue:

window.blit(init_background,(0,0))

foreventinpygame.event.get():

ifevent.type==pygame.QUIT:

exit_end()

#定义标题

pygame.display.set_caption("贪吃蛇大冒险")

#定义提示文字

font=pygame.font.SysFont("simHei",40)

fontsurf=font.render('游戏结束!你的得分为:%s'%score,False,black)

window.blit(fontsurf,(250,100))

button("返回主菜单",370,300,200,40,blue,brightred,into_game)

button("退出游戏",370,470,200,40,red,brightred,exit_end)

pygame.display.update()

clock.tick(20)

4.exit_end()函数,功能:在初始界面和游戏结束显示得分界面点击右上角的"×"时,直接退出整个游戏:

defexit_end():

pygame.quit()

sys.exit()

5.start_game()函数,功能:实现单人正常模式:

defstart_game():

#播放音乐

pygame.mixer.music.play(-1)

#定义存分数的全局变量

globalscore

score=0

#定义存放玩家键盘输入运动方向的变量,初始为向右

run_direction="right"

#定义贪吃蛇运动方向的变量,初始为玩家键入方向

run=run_direction

#实例化贪吃蛇和食物对象

head=Snake(randint(0,30)*20,randint(0,20)*20)

#实例化蛇身长度为2个单位

snake_body=[Snake(head.x,head.y+20),Snake(head.x,head.y+40)]

#实例化食物列表,列表随着其中食物被吃掉应该不断缩短

food_list=[Food(randint(0,45)*20,randint(0,28)*20,(randint(10,255),randint(10,255),randint(10,255)))]

foriinrange(1,24):

food_list.append(Food(randint(0,45)*20,randint(0,28)*20,(randint(10,255),randint(10,255),randint(10,255))))

#实例化单个食物,方便循环内生成单个新食物

food=Food(randint(0,45)*20,randint(0,28)*20,(randint(10,255),randint(10,255),randint(10,255)))

whileTrue:

window.blit(background,(0,0))

#监听玩家键盘输入的运动方向值,并根据输入转为up、down、right或left,方便程序中调用

#pygame.event.get()返回一个列表,存放本次game执行中程序遇到的一连串事件(按时间顺序依次存放)

foreventinpygame.event.get():

#pygame.QUIT事件是指用户点击窗口右上角的"×"

ifevent.type==pygame.QUIT:

#显示结果界面

show_end()

#若事件类型是按下键盘,分↑↓←→四种情况讨论

elifevent.type==pygame.KEYDOWN:

#若事件类型是按下键盘↑

#key是键值,表示按下去的键值是什么

ifevent.key==ord('w'):

run_direction="up"

#若事件类型是按下键盘↓

ifevent.key==ord('s'):

run_direction="down"

#若事件类型是按下键盘←

ifevent.key==ord('a'):

run_direction="left"

#若事件类型是按下键盘→

ifevent.key==ord('d'):

run_direction="right"

#绘制初始化的25个食物图像(24+1=25)

#随着该列表中的食物被吃掉,列表应该不断pop以清除已经被吃的事物

foriteminfood_list:

draw_food(item)

#绘制被贪吃蛇吃掉后新增的食物图像

draw_food(food)

#绘制蛇头图像

draw_snake(black,head)

#绘制蛇身图像

foriteminsnake_body:

draw_snake(blue,item)

#判断贪吃蛇原运动方向(run)与玩家键盘输入的运动方向(run_direction)是否违反正常运动情况

ifrun=="up"andnotrun_direction=="down":

run=run_direction

elifrun=="down"andnotrun_direction=="up":

run=run_direction

elifrun=="left"andnotrun_direction=="right":

run=run_direction

elifrun=="right"andnotrun_direction=="left":

run=run_direction

#插入蛇头位置到蛇身列表中

snake_body.insert(0,Snake(head.x,head.y))

#根据玩家键入方向进行蛇头xy的更新

ifrun=="up":

head.y-=20

elifrun=="down":

head.y+=20

elifrun=="left":

head.x-=20

elifrun=="right":

head.x+=20

#判断是否死亡

die_flag=False

#遍历存放贪吃蛇位姿的列表,从第1个开始,(第0个位蛇头)

forbodyinsnake_body[1:]:

#如果蛇头的xy和蛇身xy相等,则判定相撞,设置flag为ture

ifhead.x==body.xandhead.y==body.y:

die_flag=True

#若蛇头的xy在显示窗体外,或flag为true,则显示结束界面,并退出游戏

ifdie_flag==Trueorhead.x0orhead.x960orhead.y0orhead.y600:

#停止播放音乐

pygame.mixer.music.stop()

show_end()

#die_snake(head,snake_body)

#判断蛇头和食物坐标,若相等,则加分,并生成新的食物

#定义标志,表明是否找到和蛇头相等的事物

globalflag

flag=0

#如果蛇头和食物重合

foriteminfood_list:

ifhead.x==item.xandhead.y==item.yorhead.x==food.xandhead.y==food.y:

flag=1

score+=1

#弹出被吃掉的这个食物

food_list.pop(food_list.index(item))

#再产生一个食物

food=new_food(head)

#把新食物插入food_list,下一次循环中会更新绘制食物全体

food_list.append(food)

break

ifflag==0:

snake_body.pop()

font=pygame.font.SysFont("simHei",25)

mode_title=font.render('正常模式',False,grey)

socre_title=font.render('得分:%s'%score,False,grey)

window.blit(mode_title,(50,30))

window.blit(socre_title,(50,65))

#更新蛇头蛇身和食物的数据

pygame.display.update()

#通过帧率设置贪吃蛇速度

clock.tick(8)

6.start_kgame()函数,功能:实现单人穿墙模式:

defstart_kgame_single():

#播放音乐

pygame.mixer.music.play(-1)

globalscore

score=0

#定义存放玩家键盘输入运动方向的变量,初始为向右

run_direction="right"

#定义贪吃蛇运动方向的变量,初始为玩家键入方向

run=run_direction

#实例化蛇头、蛇身、食物对象

head=Snake(160,160)

#实例化蛇身

snake_body=[Snake(head.x,head.y+20),Snake(head.x,head.y+40),Snake(head.x,head.y+60)]

#实例化食物列表,列表随着其中食物被吃掉应该不断缩短

food_list=[Food(randint(0,45)*20,randint(0,28)*20,(randint(10,255),randint(10,255),randint(10,255)))]

foriinrange(1,24):

food_list.append(Food(randint(0,45)*20,randint(0,28)*20,(randint(10,255),randint(10,255),randint(10,255))))

#实例化单个食物,方便循环内生成单个新食物

food=Food(randint(0,45)*20,randint(0,28)*20,(randint(10,255),randint(10,255),randint(10,255)))

#死循环,监听键盘键值

whileTrue:

window.blit(background,(0,0))

foreventinpygame.event.get():

ifevent.type==pygame.QUIT:

show_end_single()

elifevent.type==pygame.KEYDOWN:

#若事件类型是按下键盘w

#key是键值,表示按下去的键值是什么

ifevent.key==ord('w'):

run_direction="up"

#若事件类型是按下键盘s

ifevent.key==ord('s'):

run_direction="down"

#若事件类型是按下键盘a

ifevent.key==ord('a'):

run_direction="left"

#若事件类型是按下键盘d

ifevent.key==ord('d'):

run_direction="right"

#绘制初始化的25个食物图像(24+1=25)

#随着该列表中的食物被吃掉,列表应该不断pop以清除已经被吃的事物

foriteminfood_list:

draw_food(item)

#绘制被贪吃蛇吃掉后新增的食物图像

draw_food(food)

#绘制蛇头图像

draw_snake(black,head)

#绘制蛇身图像

foriteminsnake_body:

draw_snake(blue,item)

#判断贪吃蛇原运动方向与玩家键盘输入的运动方向是否违反正常运动情况

ifrun=="up"andnotrun_direction=="down":#若运动方向为向上,玩家输入运动方向向下,则违背贪吃蛇正常运动情况

run=run_direction

elifrun=="down"andnotrun_direction=="up":

run=run_direction

elifrun=="left"andnotrun_direction=="right":

run=run_direction

elifrun=="right"andnotrun_direction=="left":

run=run_direction

#插入蛇头位置到蛇身列表中

snake_body.insert(0,Snake(head.x,head.y))

#根据玩家键入方向进行蛇头xy的更新

ifrun=="up":

head.y-=20

elifrun=="down":

head.y+=20

elifrun=="left":

head.x-=20

elifrun=="right":

head.x+=20

#穿墙实现

#定义标志位

die_flag=False

#遍历,蛇头碰到蛇身时,flag为true退出游戏

forbodyinsnake_body[1:]:

ifhead.x==body.xandhead.y==body.y:

die_flag=True

ifdie_flag:

pygame.mixer.music.stop()

show_end()

else:

#当蛇头穿出窗体时有四种情况,分别讨论

ifhead.x0:

head.x=960

ifhead.x960:

head.x=0

ifhead.y0:

head.y=600

ifhead.y600:

head.y=0

#判断蛇头和食物坐标,若相等,则加分,并生成新的食物

#定义标志,表明是否找到和蛇头相等的事物

globalflag

flag=0

#如果蛇头和食物重合

foriteminfood_list:

ifhead.x==item.xandhead.y==item.yorhead.x==food.xandhead.y==food.y:

flag=1

score+=1

#弹出被吃掉的这个食物

food_list.pop(food_list.index(item))

#再产生一个食物

food=new_food(head)

#把新食物插入food_list,下一次循环中会更新绘制食物全体

food_list.append(food)

break

#若没吃到食物则从蛇身pop一个元素以更新蛇位置

ifflag==0:

snake_body.pop()

font=pygame.font.SysFont("simHei",25)

mode_title=font.render('穿墙模式',False,grey)

socre_title=font.render('得分:%s'%score,False,grey)

window.blit(mode_title,(50,30))

window.blit(socre_title,(50,65))

#绘制更新

pygame.display.update()

#通过帧率设置贪吃蛇速度

clock.tick(8)

7.button()函数,功能:实现按钮样式设计和响应鼠标操作:

defbutton(msg,x,y,w,h,ic,ac,action=None):

#获取鼠标位置

mouse=pygame.mouse.get_pos()

#获取鼠标点击情况

click=pygame.mouse.get_pressed()

ifx+wmouse[0]xandy+hmouse[1]y:

pygame.draw.rect(window,ac,(x,y,w,h))

ifclick[0]==1andaction!=None:

action()

else:

pygame.draw.rect(window,ic,(x,y,w,h))

#设置按钮中的文字样式和居中对齐

font=pygame.font.SysFont('simHei',20)

smallfont=font.render(msg,True,white)

smallrect=smallfont.get_rect()

smallrect.center=((x+(w/2)),(y+(h/2)))

window.blit(smallfont,smallrect)

8.into_game()函数,功能:实现游戏初始界面,选择模式:

definto_game():

whileTrue:

window.blit(init_background,(0,0))

foreventinpygame.event.get():

ifevent.type==pygame.QUIT:

exit_end()

#设置字体

font=pygame.font.SysFont("simHei",50)

#初始界面显示文字

fontsurf=font.render('欢迎来到贪吃蛇大冒险!',True,black)#文字

fontrect=fontsurf.get_rect()

fontrect.center=((480),200)

window.blit(fontsurf,fontrect)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论