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