阅读 118

pygame实现贪吃蛇游戏

这篇文章主要为大家详细介绍了pygame实现贪吃蛇游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了pygame实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下

为了简化起见,游戏素材暂定为两张简单的图片(文中用的是30*30)。
大家很方便就能制作。

背景也是纯黑填充。
各种音乐字体特效玩法场景等大家可以自由发挥


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import pygame
import sys
import random
fps=6    #设置帧率
move={'up':[-30,0],'down':[30,0],'left':[0,-30],'right':[0,30]}  #上下左右映射为值
    
class MOCCASIN(object):  #蛇类
    def __init__(self):
        self.scheme=pygame.image.load('moccasin.png')
        self.x=int(20*random.random())*30
        self.y=int(20*random.random())*30
        self.body=[[self.x,self.y],[self.x,self.y+30],[self.x,self.y+60]]
        self.direct='up'
        
    def update(self):
        self.x+=move[self.direct][1]
        self.y+=move[self.direct][0]
        self.body.insert(0,[self.x,self.y])
 
class FOOD(object):  #食物类
    def __init__(self):
        self.scheme=pygame.image.load('food.png')
        self.coordinate=(int(20*random.random())*30,int(20*random.random())*30)
 
def updateMap():  #场景更新
    screen.fill((0,0,0))
    moccasin.update()
    screen.blit(food.scheme,food.coordinate)
    for x in moccasin.body:
        screen.blit(moccasin.scheme,tuple(x)+(30,30))
 
def bitself():  #是否咬到自己
    dct={}
    for ll in moccasin.body:
        if tuple(ll) in dct :return True
        dct[tuple(ll)]=1
    return False       
         
if __name__=='__main__':
    pygame.init()
    pygame.display.set_caption('「moccasin」')
    screen=pygame.display.set_mode((600,600))
 
    food=FOOD()
    moccasin=MOCCASIN()
    clk=pygame.time.Clock()
 
    while True:
        clk.tick(fps)
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                sys.exit()
            elif event.type==pygame.KEYDOWN:
                if event.key==pygame.K_UP and moccasin.direct!='down':
                    moccasin.direct='up'
                if event.key==pygame.K_DOWN and moccasin.direct!='up':
                    moccasin.direct='down'
                if event.key==pygame.K_LEFT and moccasin.direct!='right':
                    moccasin.direct='left'
                if event.key==pygame.K_RIGHT and moccasin.direct!='left':
                    moccasin.direct='right'
                     
        if not (0<=moccasin.x<600 and 0<=moccasin.y<600) or bitself(): 
            break;                                      #检测蛇是否死亡
         
        if food.coordinate==tuple(moccasin.body[0]): #是否吃到食物
            food=FOOD()
        else: moccasin.body.pop()
             
        updateMap() #更新地图
        pygame.display.update()
         
    #蛇死亡,游戏结束
    screen.fill((0,0,0))
    tips="失 败"
    tipsFont=pygame.font.Font('C:\Windows\Fonts\msyh.ttc',100)
    tipsSurf=tipsFont.render(tips,1,(255,255,255))
    screen.blit(tipsSurf,(screen.get_width()/2-tipsSurf.get_width()/2,256))
    pygame.display.flip()

以上就是本文的全部内容,希望对大家的学习有所帮助

原文链接:https://blog.csdn.net/qq_51449531/article/details/122441860


文章分类
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐