class Snake: def __init__(self, position): self.position = position self.head = SnakeHead(position) self.tail = SnakeTail() self.tail.add_element_to_tail((position[0] - 16, position[1])) def render(self, surface): surface.blit(self.head.image, self.head.rect) for tail_element in self.tail.elements: surface.blit(tail_element["image"], tail_element["rect"]) def update(self, direction): old_position = self.head.update(direction) if(self._collide_head()): return False self.position = self.head.rect.topleft for tail_element in self.tail.elements: temp = tail_element["rect"].topleft tail_element["rect"].topleft = old_position old_position = temp return True def collide(self, point): if self.head.rect.collidepoint(point): return True tail = self.tail.elements return any([elem for elem in tail if elem["rect"].collidepoint(point)]) def _collide_head(self): point = self.head.rect.topleft tail = self.tail.elements return any([elem for elem in tail if elem["rect"].collidepoint(point)]) def grow(self, grow_rate): tail_last_point = self.tail.elements[-1]["rect"].topleft while grow_rate: grow_rate -= 1 self.tail.add_element_to_tail(tail_last_point) def length(self): return len(self.tail.elements) + 1 def get_snake_coords(self): head = self.head.rect.topleft tail = [element["rect"].topleft for element in self.tail.elements] tail.append(head) return tail def load_snake_from_coord(self, coords): self.head = SnakeHead(coords.pop(-1)) self.tail = SnakeTail() for point in coords: self.tail.add_element_to_tail(point)
def move_snake(snakes, gs_settings, screen): snake_head = snakes[HEAD] new_head = SnakeHead(gs_settings, screen) if snake_head.moving_direction == "right": attr_set(new_head, snake_head.rect.centerx + 10, snake_head.rect.centery, "right") elif snake_head.moving_direction == "left": attr_set(new_head, snake_head.rect.centerx - 10, snake_head.rect.centery, "left") elif snake_head.moving_direction == "up": attr_set(new_head, snake_head.rect.centerx, snake_head.rect.centery - 10, "up") elif snake_head.moving_direction == "down": attr_set(new_head, snake_head.rect.centerx, snake_head.rect.centery + 10, "down") snakes.insert(0, new_head)
def run_game(): # 初始化游戏并创建一个项目 pygame.init() gs_settings = Settings() screen = pygame.display.set_mode( (gs_settings.screen_width, gs_settings.screen_height)) pygame.display.set_caption("Snake") snake_speed_clock = pygame.time.Clock() # 创建一个蛇头 snake_head = SnakeHead(gs_settings, screen) food = Food(screen, gs_settings) #创建蛇的身体 snakes = [snake_head] while True: #监视键盘和鼠标事件 gf.check_events(snakes[0]) gf.move_snake(snakes, gs_settings, screen) gf.check_collision(food, snakes) # 每次循环时都重绘制屏幕 gf.update_screen(gs_settings, screen, snakes, food, snake_speed_clock)
def __init__(self, start_x, start_y, initial_length = 10): self.velocity = Point(1, 0) self.head = SnakeHead(Point(start_x, start_y)) self.tail = self.head self.length = initial_length self.generate_initial_tail(initial_length)
class Snake: def __init__(self, start_x, start_y, initial_length = 10): self.velocity = Point(1, 0) self.head = SnakeHead(Point(start_x, start_y)) self.tail = self.head self.length = initial_length self.generate_initial_tail(initial_length) def generate_initial_tail(self, length): for i in range(length): self.tail = self.tail.append_segment() def handle_user_input(self, pressed_char): if pressed_char == ord('w'): self.velocity = Point(0, -1) elif pressed_char == ord('a'): self.velocity = Point(-1, 0) elif pressed_char == ord('s'): self.velocity = Point(0, 1) elif pressed_char == ord('d'): self.velocity = Point(1, 0) def is_collision_with_arena(self, arena_width, arena_height): if self.head.position.x >= arena_width-1 or \ self.head.position.x <= 0 or \ self.head.position.y >= arena_height-1 or \ self.head.position.y <= 0: return True return False def is_collision_with_self(self): first_collidable = self.head.next_segment if first_collidable is not None: if first_collidable.find_first_at_position(self.head.position) is not None: return True return False def update(self): self.last_tail_position = self.tail.position new_head_pos = self.head.position.add(self.velocity) self.head.advance(new_head_pos) def eat_if_possible(self, foods): edible = filter(lambda f: f.position.equals(self.head.position), foods) for e in edible: self.tail = self.head.append_segment() self.length += 1 self.head.has_gulp = True foods.remove(e) def draw(self, pad): pad.addstr(self.last_tail_position.y, self.last_tail_position.x, " ") self.head.draw(pad) def toggle_style(self): self.head.toggle_style()
import pygame from snake_head import SnakeHead from snake_body import Body from foody import Food pygame.init() screen = pygame.display.set_mode((500, 600)) clock = pygame.time.Clock() pygame.display.set_caption("Trò chơi con rắn") snake = SnakeHead(50, 100, 20, 20, 10) body = Body(snake) food = Food(10, 10) # Mainloop while True: clock.tick(10) screen.fill((155, 155, 155)) snake.createSnake(screen) body.createBody(screen) food.createFood(screen) snake.move() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() collide = snake.getRect().colliderect(food.getRect()) if collide == True: # print("Snake Length: ", body.width) body.setLength() food.newPosition() print("Food postion", food.x, food.y)
def __init__(self, position): self.position = position self.head = SnakeHead(position) self.tail = SnakeTail() self.tail.add_element_to_tail((position[0] - 16, position[1]))
def load_snake_from_coord(self, coords): self.head = SnakeHead(coords.pop(-1)) self.tail = SnakeTail() for point in coords: self.tail.add_element_to_tail(point)