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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
| import random import sys import time import pygame from pygame.locals import * from collections import deque
LIGHT = (100, 100, 100) DARK = (200, 200, 200) BLACK = (0, 0, 0) RED = (200, 30, 30) BGCOLOR = (40, 40, 60) GAMEOVER = 0 GAMERUN = 1
class Snake: def __init__(self,w,h): self.speed = 0.1 self.snake = [(0,0)] self.body = set(self.snake) self.head = (0,0) self.direction = 'R' self.snakeColor = DARK self.foodColor = RED self.foods = set() self.state = GAMERUN self.score = 0 self.w = w self.h = h self.add_food()
def set_snake_color(self,rgb): self.color = rgb
def set_snake_direction(self,dir): if (dir == 'L' and self.direction == 'R') or \ (dir == 'R' and self.direction == 'L') or \ (dir == 'U' and self.direction == 'D') or \ (dir == 'D' and self.direction == 'U'): return False self.direction = dir
def get_score(self): return len(self.snake) - 1
def snake_move(self): d = {"L":(0,-1),"R":(0,1),"U":(-1,0),"D":(1,0)} x = self.head[0] + d[self.direction][0] y = self.head[1] + d[self.direction][1]
if x < 0 or y < 0 or x >= self.h or y >= self.w: self.state = GAMEOVER self.scroe = -1 return -1
curr = (x,y) self.head = (x,y) self.snake.append(curr)
if len(self.foods) > 0 and curr in self.foods: self.foods.remove(curr) self.body.add(curr) else: self.body.remove(self.snake[0]) self.snake.pop(0) if curr in self.body: self.state = GAMEOVER self.scroe = -1 return -1 else: self.body.add(curr)
if len(self.foods) == 0: self.add_food()
return len(self.snake)-1
def add_food(self): nums = random.randint(1,10) for i in range(nums): self.foods.add(self.creat_food())
def creat_food(self): food_x = random.randint(0,self.h) food_y = random.randint(0,self.w) while (food_x, food_y) in self.body or (food_x, food_y) in self.foods: food_x = random.randint(0,self.w) food_y = random.randint(0,self.h)
return (food_x,food_y)
class Game: def __init__(self,w,h,sz = 20): self.width = w self.high = h self.textHigh = 2 self.sz = sz self.line = 1 self.backGround = BGCOLOR self.snakeColor = DARK self.foodColor = RED self.textColor = RED self.state = GAMERUN self.score = 0 self.speed = 1.0 self.last_move_time = time.time() self.snake = Snake(w,h-self.textHigh)
pygame.init() self.screen = pygame.display.set_mode((w*sz, h*sz)) pygame.display.set_caption('Gluttonous snake') self.font1 = pygame.font.SysFont('SimHei', 24) self.font2 = pygame.font.SysFont('arial', 72)
def text_draw(self): imgText = self.font1.render(f'speed: {int(1/self.speed)}',True,pygame.Color(255, 255, 255)) self.screen.blit(imgText,(160,10)) imgText = self.font1.render(f'score: {self.score}',True,pygame.Color(255, 255, 255)) self.screen.blit(imgText,(self.width*self.sz-160,10))
def game_draw(self): self.screen_draw() self.snake_draw() self.food_draw() self.line_draw() self.text_draw()
def speed_up(self): if self.speed < 0.1: return self.speed = self.speed - 0.05
def speed_down(self): self.speed = self.speed + 0.05
def screen_draw(self): self.screen.fill(self.backGround)
def snake_draw(self): for x,y in self.snake.body: pygame.draw.rect(self.screen,self.snakeColor,(y*self.sz,(x+self.textHigh)*self.sz,self.sz,self.sz),0)
def line_draw(self): pygame.draw.rect(self.screen,LIGHT,(0,0,self.width*self.sz,self.textHigh*self.sz),0) for x in range(self.width): p1 = (x*self.sz,self.textHigh*self.sz) p2 = (x*self.sz, self.high*self.sz) pygame.draw.line(self.screen, BLACK,p1, p2, 1) for y in range(self.textHigh,self.high): p1 = (0,y*self.sz) p2 = (self.width*self.sz, y*self.sz) pygame.draw.line(self.screen, BLACK, p1, p2, 1)
def food_draw(self): for (x,y) in self.snake.foods: pygame.draw.rect(self.screen,self.foodColor,(y*self.sz,(x+self.textHigh)*self.sz,self.sz,self.sz),0)
def scree_update(self): pygame.display.update() def game_over(self): imgText = self.font2.render(f'game over',True,pygame.Color(200,30,30)) self.screen.blit(imgText,(self.width/2*self.sz-100,self.high/2*self.sz)) def run(self): while True: self.game_draw() self.scree_update()
for event in pygame.event.get(): if event.type == QUIT: sys.exit() elif event.type == KEYDOWN: if event.key in (K_w, K_UP): self.snake.set_snake_direction('U') elif event.key in (K_s, K_DOWN): self.snake.set_snake_direction('D') elif event.key in (K_a, K_LEFT): self.snake.set_snake_direction('L') elif event.key in (K_d, K_RIGHT): self.snake.set_snake_direction('R') elif event.key in (K_PLUS, K_EQUALS): self.speed_up() elif event.key in (K_MINUS,K_UNDERSCORE): self.speed_down() if self.state is not GAMEOVER: curTime = time.time() if curTime - self.last_move_time > self.speed: res = self.snake.snake_move() if res < 0: self.state = GAMEOVER else: self.last_move_time = curTime self.score = res
if self.state == GAMEOVER: self.game_over() self.scree_update()
game = Game(40,30) game.run()
|