def _handle_player_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: key = event.key #left = -1 #right = 1 #up = -1 #down = 1 if key == self.actions["left"] and self.player.dir.x != 1: self.player.dir = vec2d((-1, 0)) if key == self.actions["right"] and self.player.dir.x != -1: self.player.dir = vec2d((1, 0)) if key == self.actions["up"] and self.player.dir.y != 1: self.player.dir = vec2d((0, -1)) if key == self.actions["down"] and self.player.dir.y != -1: self.player.dir = vec2d((0, 1)) self.player.update_head = True
def __init__(self, speed, length, pos_init, width, color, SCREEN_WIDTH, SCREEN_HEIGHT): self.dir = vec2d((1, 0)) self.speed = speed self.pos = vec2d(pos_init) self.color = color self.width = width self.length = length self.body = [] self.update_head = True # build our body up for i in range(self.length): self.body.append( # makes a neat "zapping" in effect SnakeSegment( (self.pos.x - (width) * i, self.pos.y), self.width, self.width, tuple([c - 100 for c in self.color]) if i == 0 else self.color)) # we dont add the first few because it cause never actually hit it self.body_group = pygame.sprite.Group() self.head = self.body[0]
def update(self, dt): for i in range(self.length - 1, 0, -1): scale = 0.1 self.body[i].pos = vec2d((((1.0 - scale) * self.body[i - 1].pos.x + scale * self.body[i].pos.x), ((1.0 - scale) * self.body[i - 1].pos.y + scale * self.body[i].pos.y))) self.body[i].rect.center = (self.body[i].pos.x, self.body[i].pos.y) self.head.pos.x += self.dir.x * self.speed * dt self.head.pos.y += self.dir.y * self.speed * dt self.update_hitbox()
def new_position(self, snake): new_pos = snake.body[0].pos snake_body = [s.pos for s in snake.body] while (new_pos in snake_body): _x = self.rng.choice( range(self.width * 2, self.SCREEN_WIDTH - self.width * 2, self.width)) _y = self.rng.choice( range(self.width * 2, self.SCREEN_HEIGHT - self.width * 2, self.width)) new_pos = vec2d((_x, _y)) self.pos = new_pos self.rect.center = (self.pos.x, self.pos.y)
def __init__(self, pos_init, width, height, color): pygame.sprite.Sprite.__init__(self) self.pos = vec2d(pos_init) self.color = color self.width = width self.height = height image = pygame.Surface((width, height)) image.fill((0, 0, 0)) image.set_colorkey((0, 0, 0)) pygame.draw.rect(image, color, (0, 0, self.width, self.height), 0) self.image = image # use half the size self.rect = pygame.Rect(pos_init, (self.width / 2, self.height / 2)) self.rect.center = pos_init
def __init__(self, pos_init, width, color, SCREEN_WIDTH, SCREEN_HEIGHT, rng): pygame.sprite.Sprite.__init__(self) self.pos = vec2d(pos_init) self.color = color self.SCREEN_WIDTH = SCREEN_WIDTH self.SCREEN_HEIGHT = SCREEN_HEIGHT self.width = width self.rng = rng image = pygame.Surface((width, width)) image.fill((0, 0, 0, 0)) image.set_colorkey((0, 0, 0)) pygame.draw.rect(image, color, (0, 0, self.width, self.width), 0) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init