Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import sys
- import pygame
- from pygame.locals import *
- #Tạo khung hiển thị game
- width = 800
- height = 700
- display_surf = pygame.display.set_mode((width, height))
- pygame.display.set_caption("Snake")
- #Define màu cho game
- GREEN = (0, 255, 0)
- YELLOW = (255, 140, 0)
- BLACK = (255, 255, 255)
- fps_clock = pygame.time.Clock()
- #Điểm
- class Point:
- def __init__(self, x, y, r):
- self.center_x = x
- self.center_y = y
- self.radius = r
- def draw(self):
- pygame.draw.circle(display_surf, YELLOW, (self.center_x, self.center_y), self.radius)
- #Con rắn
- class Snake:
- def __init__(self, part_position, radius):
- self.part_position = part_position
- self.radius = radius
- #Khởi tạo part = [0, 0]
- self.part = [0, 0]
- def draw(self):
- for part in self.part_position:
- #Nếu chỉ để hàm part trong hàm vẽ thì sẽ bị bug 2 con rắn
- part_element = map(int, part)
- pygame.draw.circle(display_surf, GREEN, tuple(part_element), self.radius)
- def move(self, direction):
- #Con rắn di chuyển bằng cách bỏ block cuối và đưa lên đầu
- #Insert khúc vào vị trí đầu, có cùng x/y với với block 0 và tọa độ còn lại thì +- 2 * self.radius
- #self.part_position[0][0] là self.part_position[0].x
- if direction == "forward":
- self.part_position.insert(0, [self.part_position[0][0], self.part_position[0][1] - 2 * self.radius])
- elif direction == "backward":
- self.part_position.insert(0, [self.part_position[0][0], self.part_position[0][1] + 2 * self.radius])
- elif direction == "right":
- self.part_position.insert(0, [self.part_position[0][0] + 2 * self.radius, self.part_position[0][1]])
- elif direction == "left":
- self.part_position.insert(0, [self.part_position[0][0] - 2 * self.radius, self.part_position[0][1]])
- #Hàm self.part = [0, 0] lưu trữ giá trị của khúc cuối để sau này khi ăn point chỉ cần thêm vào
- self.part = self.part_position[-1]
- #Xóa khúc cuối
- self.part_position.pop(-1)
- def hit_body(self):
- hit = False
- for i in range(len(self.part_position) - 1):
- if self.part_position[-1] == self.part_position[i]:
- hit = True
- break
- if hit:
- return True
- else:
- return False
- def hit_wall(self):
- if int(self.part_position[0][0] - self.radius) <= 0 or int(self.part_position[0][0] + self.radius) \
- >= width or int(self.part_position[0][1] - self.radius) <= 0 \
- or int(self.part_position[0][1] + self.radius) >= height:
- return True
- else:
- return False
- def hit_point(self, point):
- if float(point.center_y) - float(point.radius + self.radius) <= self.part_position[0][1] \
- <= float(point.center_y) + float(point.radius + self.radius) \
- or float(point.center_x) - float(point.radius + self.radius) <= self.part_position[0][0] \
- <= float(point.center_x) + float(point.radius + self.radius):
- return True
- else:
- return False
- def eat_point(self, point):
- # Random vị trí của điểm
- point.center_x = random.randrange(10, width - 10)
- point.center_y = random.randrange(10, height - 10)
- # Thêm một khúc vào đuôi nếu ăn điểm. self.part = self.part_position[-1]
- self.part_position.insert(-1, self.part)
- class Game:
- def __init__(self, snake, point, speed):
- self.snake = snake
- self.point = point
- self.speed = speed
- def draw_arena(self):
- # Xóa hết để sau mỗi lần vẽ lại
- display_surf.fill((0, 0, 0))
- pygame.draw.rect(display_surf, BLACK, (0, 0, width, height), 10)
- # Vẽ lại
- self.snake.draw()
- self.point.draw()
- def update(self, fps, direction):
- #Update các giá trị
- if self.snake.hit_point(self.point):
- self.snake.eat_point(self.point)
- #Tăng speed
- fps += self.speed
- self.snake.move(direction)
- pygame.display.update()
- fps_clock.tick(fps)
- return fps
- def main():
- pygame.init()
- snake = Snake([[width / 2, height / 2]], 10)
- point = Point(random.randrange(10, width - 10), random.randrange(10, height - 10), 10)
- direction = "Right"
- game = Game(snake, point, 1)
- die = False
- fps = 15
- while True:
- if snake.hit_body() or snake.hit_wall():
- die = True
- for event in pygame.event.get():
- #Dùng phím esc để thoát game, tránh làm máy giật lag
- #Nếu direction không khác hướng ngược lại hướng mình muốn di chuyển thì rắn có thể đi ngược lại
- #và tự cắn vào bản thân
- if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
- die = True
- if event.type == KEYDOWN and event.key == K_w and direction != "backward":
- direction = "forward"
- elif event.type == KEYDOWN and event.key == K_s and direction != "forward":
- direction = "backward"
- elif event.type == KEYDOWN and event.key == K_a and direction != "right":
- direction = "left"
- elif event.type == KEYDOWN and event.key == K_d and direction != "left":
- direction = "right"
- if die:
- pygame.quit()
- sys.exit()
- game.draw_arena()
- fps = game.update(fps, direction)
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment