https://i.cnblogs.com/settings

python之游戏开发-贪吃蛇

  1 #!/usr/bin/env python
  2 # coding=utf-8
  3 import pygame
  4 import sys
  5 import random
  6 
  7 # 全局定义,screen的长和宽
  8 SCREEN_X = 600
  9 SCREEN_Y = 600
 10 
 11 
 12 # 蛇类
 13 # 点以25为单位
 14 class Snake(object):  # 定义一个类--Snake,默认使用object类,如果你有更好的话,可以替换它
 15     # 初始化各种需要的属性 [开始时默认向右/身体块x5]
 16     def __init__(self):  # 定义init方法,第一个参数永远都是self本身,在__init__方法内部,可以把各种属性绑定到self,因为self指向创建实例的本身。
 17         # 有了__init__方法,在创建实例的时候,就不能传入空的参数来,必须传入与__init__方法匹配的参数,但是self不需要穿
 18         # 举例,如果是def __init__(self,name,score):这种类型的话,那么self不需要,但是name和score必须要有的
 19         self.dirction = pygame.K_RIGHT
 20         self.body = []
 21         for x in range(5):
 22             self.addnode()  # 调用addnode函数,增加身体,循环5次后,蛇身初始值为5个node
 23 
 24     # 增加贪吃蛇身体长度的函数,无论何时 都在前端增加蛇块
 25     def addnode(self):
 26         left, top = (0, 0)  # 默认是0
 27         if self.body:
 28             left, top = (self.body[0].left, self.body[0].top)
 29         node = pygame.Rect(left, top, 25, 25)  # 一个node节点的大小
 30         if self.dirction == pygame.K_LEFT:
 31             node.left -= 25
 32         elif self.dirction == pygame.K_RIGHT:
 33             node.left += 25
 34         elif self.dirction == pygame.K_UP:
 35             node.top -= 25
 36         elif self.dirction == pygame.K_DOWN:
 37             node.top += 25
 38         self.body.insert(0, node)  # 插入节点
 39 
 40     # 删除最后一个块,pop默认从最后弹出元素
 41     def delnode(self):
 42         self.body.pop()
 43 
 44     # 死亡判断
 45     def isdead(self):
 46         # 撞墙,身体top部位不在SCREEN中的X或者Y值中(0,600),认为撞墙死亡
 47         if self.body[0].x not in range(SCREEN_X):
 48             return True
 49         if self.body[0].y not in range(SCREEN_Y):
 50             return True
 51             # 撞自己,头部和身体任意一部分碰撞。body[0]是第一个元素,body[1:]取的是从第二个元素开始,到完结
 52         if self.body[0] in self.body[1:]:
 53             return True
 54         return False
 55 
 56     # 移动!
 57     def move(self):
 58         self.addnode()
 59         self.delnode()
 60 
 61     # 改变方向 但是左右、上下不能被逆向改变
 62     def changedirection(self, curkey):
 63         LR = [pygame.K_LEFT, pygame.K_RIGHT]
 64         UD = [pygame.K_UP, pygame.K_DOWN]
 65         if curkey in LR + UD:
 66             if (curkey in LR) and (self.dirction in LR):
 67                 return
 68             if (curkey in UD) and (self.dirction in UD):
 69                 return
 70             self.dirction = curkey
 71 
 72 
 73 # 食物类
 74 # 方法: 放置/移除
 75 # 点以25为单位
 76 class Food:
 77     def __init__(self):
 78         self.rect = pygame.Rect(-25, 0, 25, 25)
 79 
 80     def remove(self):
 81         self.rect.x = -25
 82 
 83     def set(self):
 84         if self.rect.x == -25:
 85             allpos = []
 86             # 不靠墙太近 25 ~ SCREEN_X-25 之间
 87             for pos in range(25, SCREEN_X - 25, 25):
 88                 allpos.append(pos)
 89             self.rect.left = random.choice(allpos)
 90             self.rect.top = random.choice(allpos)
 91             print(self.rect)
 92 
 93 
 94 def show_text(screen, pos, text, color, font_bold=False, font_size=60, font_italic=False):
 95     # 获取系统字体,并设置文字大小
 96     cur_font = pygame.font.SysFont("宋体", font_size)
 97     # 设置是否加粗属性
 98     cur_font.set_bold(font_bold)
 99     # 设置是否斜体属性
100     cur_font.set_italic(font_italic)
101     # 设置文字内容
102     text_fmt = cur_font.render(text, 1, color)
103     # 绘制文字
104     screen.blit(text_fmt, pos)
105 
106 
107 def main():
108     pygame.init()
109     screen_size = (SCREEN_X, SCREEN_Y)
110     screen = pygame.display.set_mode(screen_size)
111     pygame.display.set_caption('Snake')
112     clock = pygame.time.Clock()
113     scores = 0
114     isdead = False
115 
116     # 蛇/食物
117     snake = Snake()
118     food = Food()
119 
120     while True:
121         for event in pygame.event.get():
122             if event.type == pygame.QUIT:
123                 sys.exit()
124             if event.type == pygame.KEYDOWN:
125                 snake.changedirection(event.key)
126                 # 死后按space重新
127                 if event.key == pygame.K_SPACE and isdead:
128                     return main()
129 
130         screen.fill((255, 255, 255))
131 
132         # 画蛇身 / 每一步+1分
133         if not isdead:
134             scores += 1
135             snake.move()
136         for rect in snake.body:
137             pygame.draw.rect(screen, (20, 220, 39), rect, 0)
138 
139             # 显示死亡文字
140         isdead = snake.isdead()
141         if isdead:
142             show_text(screen, (100, 200), 'YOU DEAD!', (227, 29, 18), False, 100)
143             show_text(screen, (150, 260), 'press space to try again...', (0, 0, 22), False, 30)
144 
145             # 食物处理 / 吃到+50分
146         # 当食物rect与蛇头重合,吃掉 -> Snake增加一个Node
147         if food.rect == snake.body[0]:
148             scores += 50
149             food.remove()
150             snake.addnode()
151 
152             # 食物投递
153         food.set()
154         pygame.draw.rect(screen, (136, 0, 21), food.rect, 0)
155 
156         # 显示分数文字
157         show_text(screen, (50, 500), 'Scores: ' + str(scores), (223, 223, 223))
158 
159         pygame.display.update()
160         clock.tick(10)
161 
162 
163 if __name__ == '__main__':
164     main()

 

posted @ 2019-11-09 10:46  LLLN  阅读(367)  评论(0)    收藏  举报
Copyright © 2021 llln Powered by .NET 5.0 on Kubernetes