Python贪吃蛇代码总结
import pygame,time,random
black=(0,0,0) #定义黑色
white=(255,255,255) #定义白色
red=(255,0,0) #定义红色
pygame.init()
#游戏区域大小
dis_width=800
dis_height=600
#游戏初始化
dis=pygame.display.set_mode((dis_width,dis_height))
pygame.display.set_caption('贪吃蛇')
clock=pygame.time.Clock()
snake_block=10
snake_speed=15
font_style=pygame.font.SysFont(None,50)
def our_snake(snake_block,snake_list):
for x in snake_list:
pygame.draw.rect(dis, black,[x[0],x[1],snake_block,snake_block])
def message(msg,color):
mesg=font_style.render(msg,True,color)
dis.blit(mesg,[dis_width/6,dis_height/3])
def gameLoop(): #定义游戏循环
game_over=False
game_close=False
x1=dis_width/2
y1=dis_height/2
x1_change=0
y1_change=0
snake_List=[]
Length_of_snake=1
foodx=round(random.randrange(0,dis_width-snake_block)/10.0)*10.0
foody=round(random.randrange(0,dis_height-snake_block)/10.0)*10.0
while not game_over:
while game_close == True:
dis.fill(white)
message("你输了!按Q-退出,C-重新开始",red)
pygame.display.update()
for event in pygame.event.get():
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_q:
game_over=True
game_close=False
if event.key==pygame.K_c:
gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over=True
if event.type==pygame.KEYDOWN: #通过wasd键改变蛇的方向
if event.key==pygame.K_LEFT:
x1_change=-snake_block
y1_change=0
elif event.key==pygame.K_RIGHT:
x1_change=snake_block
y1_change=0
elif event.key==pygame.K_UP:
y1_change=-snake_block
x1_change=0
elif event.key==pygame.K_DOWN:
y1_change=snake_block
x1_change=0
if x1>=dis_width or x1<0 or y1>=dis_height or y1<0:
game_close=True
x1+=x1_change
y1+=y1_change
dis.fill(white)
pygame.draw.rect(dis, red, [foodx, foody, snake_block, snake_block])
snake_Head=[]
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
if len(snake_List)>Length_of_snake:
del snake_List[0]
for x in snake_List[:-1]:
if x==snake_Head:
game_close=True
our_snake(snake_block, snake_List)
pygame.display.update()
if x1==foodx and y1==foody:
foodx=round(random.randrange(0,dis_width-snake_block)/10.0)*10.0
foody=round(random.randrange(0,dis_height-snake_block)/10.0)*10.0
Length_of_snake+=1
clock.tick(snake_speed)
pygame.quit()
quit()
gameLoop()
贪吃蛇是一款老少皆宜的游戏,是初学者练习编程的好选择。Python作为一种优秀的编程语言,也有自己的贪吃蛇代码。Python贪吃蛇代码使用了pygame模块和一些基础的Python知识,让我们一起来探究。
Python贪吃蛇编程代码
这份Python贪吃蛇代码是基于pygame模块实现的。在代码中,我们需要导入pygame模块。在游戏循环开始前,需要定义游戏区域大小,定义黑白红三种颜色和初始蛇的大小和速度。我们还需要设置字体,以便在游戏失败时输出提示信息。游戏初始化和字体设置代码如下:
pygame.init()
#游戏区域大小
dis_width=800
dis_height=600
#游戏初始化
dis=pygame.display.set_mode((dis_width,dis_height))
pygame.display.set_caption('贪吃蛇')
clock=pygame.time.Clock()
snake_block=10
snake_speed=15
font_style=pygame.font.SysFont(None,50)
在Python贪吃蛇代码中,我们定义了三个函数,分别为our_snake()
、message()
和gameLoop()
。其中,our_snake()
函数是绘制贪吃蛇的函数;message()
函数是在游戏失败时展示提示信息的函数;gameLoop()
函数是游戏的主循环函数,其中包含了游戏画面的更新、蛇的控制、食物和身体的生成以及游戏结束的处理。这三个函数的代码如下:
def our_snake(snake_block,snake_list):
for x in snake_list:
pygame.draw.rect(dis, black,[x[0],x[1],snake_block,snake_block])
def message(msg,color):
mesg=font_style.render(msg,True,color)
dis.blit(mesg,[dis_width/6,dis_height/3])
def gameLoop(): #定义游戏循环
game_over=False
game_close=False
x1=dis_width/2
y1=dis_height/2
x1_change=0
y1_change=0
snake_List=[]
Length_of_snake=1
foodx=round(random.randrange(0,dis_width-snake_block)/10.0)*10.0
foody=round(random.randrange(0,dis_height-snake_block)/10.0)*10.0
while not game_over:
while game_close == True:
dis.fill(white)
message("你输了!按Q-退出,C-重新开始",red)
pygame.display.update()
for event in pygame.event.get():
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_q:
game_over=True
game_close=False
if event.key==pygame.K_c:
gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over=True
if event.type==pygame.KEYDOWN: #通过wasd键改变蛇的方向
if event.key==pygame.K_LEFT:
x1_change=-snake_block
y1_change=0
elif event.key==pygame.K_RIGHT:
x1_change=snake_block
y1_change=0
elif event.key==pygame.K_UP:
y1_change=-snake_block
x1_change=0
elif event.key==pygame.K_DOWN:
y1_change=snake_block
x1_change=0
if x1>=dis_width or x1<0 or y1>=dis_height or y1<0:
game_close=True
x1+=x1_change
y1+=y1_change
dis.fill(white)
pygame.draw.rect(dis, red, [foodx, foody, snake_block, snake_block])
snake_Head=[]
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
if len(snake_List)>Length_of_snake:
del snake_List[0]
for x in snake_List[:-1]:
if x==snake_Head:
game_close=True
our_snake(snake_block, snake_List)
pygame.display.update()
if x1==foodx and y1==foody:
foodx=round(random.randrange(0,dis_width-snake_block)/10.0)*10.0
foody=round(random.randrange(0,dis_height-snake_block)/10.0)*10.0
Length_of_snake+=1
clock.tick(snake_speed)
pygame.quit()
quit()
Python贪吃蛇代码框架
Python贪吃蛇代码的框架是由游戏的初始化、游戏循环和游戏结束处理三部分组成的。其中,游戏的初始化部分我们已经在Python贪吃蛇编程代码中讲解了,这里主要介绍游戏循环和游戏结束处理两部分的框架结构。 在Python贪吃蛇代码中,游戏循环部分的框架可以概括为以下四个步骤:
- 使用
pygame.event.get()
方法获取事件 - 使用
pygame.KEYDOWN
方法响应事件 - 根据wasd键改变蛇的方向
- 根据蛇的位置更新游戏并判断游戏是否结束 游戏结束处理部分的框架可以概括为以下三个步骤:
- 输出提示信息
- 响应按键事件
- 根据按键事件更新游戏状态
Python贪吃蛇代码复制
想要复制Python贪吃蛇代码,只需要在任意的Python集成开发环境中或者记事本中粘贴上述代码即可。如果要在终端中执行Python贪吃蛇代码,需要确保已经安装了pygame模块。
Python贪吃蛇代码wasd
在Python贪吃蛇代码中,我们可以通过wasd键来改变蛇的方向。具体来说,在游戏循环部分,我们可以利用pygame.KEYDOWN
方法对wasd键进行响应,根据键盘输入的不同,改变蛇的x、y方向上的速度。代码如下:
if event.type==pygame.KEYDOWN: #通过wasd键改变蛇的方向
if event.key==pygame.K_LEFT:
x1_change=-snake_block
y1_change=0
elif event.key==pygame.K_RIGHT:
x1_change=snake_block
y1_change=0
elif event.key==pygame.K_UP:
y1_change=-snake_block
x1_change=0
elif event.key==pygame.K_DOWN:
y1_change=snake_block
x1_change=0
Python贪吃蛇代码写好了怎么运行
运行Python贪吃蛇代码需要使用Python集成开发环境,比如PyCharm、Spyder等。在新建一个Python文件后,将以上代码粘贴到文件中并保存,即可通过运行这个文件来开始游戏。在开始游戏之前,请确保计算机中已经安装了pygame模块。
Python贪吃蛇代码参考文献
Python贪吃蛇代码的实现是基于pygame模块的,如果对pygame模块不熟悉,可以参考以下文档:
- Pygame官方网站:https://www.pygame.org/news
- Pygame documentation:https://www.pygame.org/docs/
- Pygame tutorials:https://www.pygame.org/wiki/tutorials