python实现贪吃蛇游戏源码
作者:心心强 时间:2021-07-22 12:45:24
本文实例为大家分享了python实现贪吃蛇的具体代码,供大家参考,具体内容如下
import pygame
import sys
import random
SCREEN_X=600
SCREEN_Y=600
class Snake(object):
def __init__(self):
self.direction=pygame.K_RIGHT
self.body = []
for i in range(5):
self.addnode()
def addnode(self):
left, top = (0, 0)
if self.body:
left, top = (self.body[0].left,self.body[0].top)
node = pygame.Rect(left,top,25,25)
if self.direction == pygame.K_RIGHT:
node.left += 25
elif self.direction == pygame.K_LEFT:
node.left -= 25
elif self.direction == pygame.K_UP:
node.top -= 25
elif self.direction == pygame.K_DOWN:
node.top += 25
self.body.insert(0,node)
def delnode(self):
self.body.pop()
def isdead(self):
if self.body[0].x not in range(SCREEN_X):
return True
if self.body[0].y not in range(SCREEN_Y):
return True
if self.body[0] in self.body[1:]:
print('in body')
return True
return False
def move(self):
self.addnode()
self.delnode()
def changeddirection(self,curkey):
LR = [pygame.K_LEFT,pygame.K_RIGHT]
UD = [pygame.K_DOWN,pygame.K_UP]
if curkey in LR+UD:
if (curkey in LR) and (self.direction in LR):
return
if (curkey in UD) and (self.direction in UD):
return
self.direction = curkey
class Food(object):
def __init__(self):
self.rect = pygame.Rect(-25, 0, 25, 25)
def remove(self):
self.rect.x = -25
def set(self):
if self.rect.x == -25:
allpos = []
for pos in range(25,SCREEN_X-25,25):
allpos.append(pos)
self.rect.left = random.choice(allpos)
self.rect.top = random.choice(allpos)
print(self.rect)
def show_text(screen,pos,text,color,font_bold = False,font_size = 60,font_italic = False):
cur_font=pygame.font.SysFont('宋体',font_size)
cur_font.set_bold(font_bold)
cur_font.set_italic(font_italic)
text_fmt = cur_font.render(text,1,color)
screen.blit(text_fmt,pos)
def main():
pygame.init()
screen_size = (SCREEN_X,SCREEN_Y)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('Snake')
clock=pygame.time.Clock()
scores=0
isdead=False
snake=Snake()
food=Food()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
#print(event)
#pressed_keys = pygame.key.get_pressed()
#if pressed_keys.count(1)>1:
# continue
print(event.key)
snake.changeddirection(event.key)
if event.key == pygame.K_SPACE and isdead:
return main()
screen.fill((255,255,255))
if not isdead:
scores +=1
snake.move()
for rect in snake.body:
pygame.draw.rect(screen,(20,220,39),rect,0)
isdead=snake.isdead()
if isdead:
show_text(screen,(100,200),'you dead!',(227,29,18),False,100)
show_text(screen,(150,260),'press space to try again...',(0,0,22),False,30)
if food.rect == snake.body[0]:
scores +=50
food.remove()
snake.addnode()
food.set()
pygame.draw.rect(screen,(136,0,21),food.rect,0)
show_text(screen,(50,500),'Scores:'+str(scores),(223,223,223))
pygame.display.update()
clock.tick(5)
if __name__ == "__main__":
main()
来源:https://blog.csdn.net/u010708028/article/details/104922573
标签:python,贪吃蛇
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于Python实现主机远程控制
2023-11-24 17:27:31
![](https://img.aspxhome.com/file/2023/5/95175_0s.gif)
python制作简单五子棋游戏
2021-09-13 06:49:47
Python用 KNN 进行验证码识别的实现方法
2021-03-29 06:58:08
![](https://img.aspxhome.com/file/2023/9/72749_0s.jpg)
[译]2009年海外Web设计风潮(上)
2009-01-23 09:21:00
![](https://img.aspxhome.com/file/UploadPic/up/3-2009webui.gif)
access MDB 转换为 Execl(ASP类)
2008-07-19 12:10:00
tab(标签)在使用时的禁忌
2009-04-16 13:06:00
![](https://img.aspxhome.com/file/UploadPic/20094/16/01-33s.jpg)
RS.GETROWS使用详解
2008-01-16 13:21:00
技巧/诀窍:在ASP.NET中重写URL
2007-09-23 12:21:00
![](https://img.aspxhome.com/file/UploadPic/20079/23/2007923122811285s.jpg)
PyQT5 QTableView显示绑定数据的实例详解
2023-09-09 19:40:40
![](https://img.aspxhome.com/file/2023/1/101121_0s.jpg)
利用python3如何给数据添加高斯噪声
2023-06-11 20:52:17
![](https://img.aspxhome.com/file/2023/8/59298_0s.png)
Django执行python manage.py makemigrations报错的解决方案分享
2021-05-23 06:58:05
asp内置对象Application详解
2007-09-19 12:08:00
Python中常用的内置函数
2023-01-06 08:15:30
![](https://img.aspxhome.com/file/2023/8/79748_0s.png)
python完成FizzBuzzWhizz问题(拉勾网面试题)示例
2022-01-31 14:41:11
Python脚本实现Web漏洞扫描工具
2023-05-01 04:18:50
python实现爱奇艺登陆密码RSA加密的方法示例详解
2022-02-10 00:41:53
![](https://img.aspxhome.com/file/2023/9/115839_0s.jpg)
怎么样才能设计出漂亮的网页?
2008-10-07 16:57:00
python 判断是否为正小数和正整数的实例
2022-06-12 01:26:04
详解python:time模块用法
2023-04-12 06:06:48
![](https://img.aspxhome.com/file/2023/4/100784_0s.png)
Tensorflow进行多维矩阵的拆分与拼接实例
2021-11-29 22:20:58
![](https://img.aspxhome.com/file/2023/2/89342_0s.jpg)