Guest User

Untitled

a guest
Jun 24th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. import random
  2. import sys
  3. import pygame
  4. from pygame.locals import *
  5.  
  6. #Tạo khung hiển thị game
  7.  
  8. width = 800
  9. height = 700
  10.  
  11. display_surf = pygame.display.set_mode((width, height))
  12. pygame.display.set_caption("Snake")
  13.  
  14. #Define màu cho game
  15.  
  16. GREEN = (0, 255, 0)
  17.  
  18. YELLOW = (255, 140, 0)
  19.  
  20. BLACK = (255, 255, 255)
  21.  
  22. fps_clock = pygame.time.Clock()
  23.  
  24. #Điểm
  25.  
  26. class Point:
  27.  
  28. def __init__(self, x, y, r):
  29. self.center_x = x
  30. self.center_y = y
  31. self.radius = r
  32.  
  33. def draw(self):
  34. pygame.draw.circle(display_surf, YELLOW, (self.center_x, self.center_y), self.radius)
  35.  
  36. #Con rắn
  37.  
  38. class Snake:
  39.  
  40. def __init__(self, part_position, radius):
  41. self.part_position = part_position
  42. self.radius = radius
  43. #Khởi tạo part = [0, 0]
  44. self.part = [0, 0]
  45.  
  46. def draw(self):
  47. for part in self.part_position:
  48. #Nếu chỉ để hàm part trong hàm vẽ thì sẽ bị bug 2 con rắn
  49. part_element = map(int, part)
  50. pygame.draw.circle(display_surf, GREEN, tuple(part_element), self.radius)
  51.  
  52. def move(self, direction):
  53. #Con rắn di chuyển bằng cách bỏ block cuối và đưa lên đầu
  54. #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
  55. #self.part_position[0][0] là self.part_position[0].x
  56. if direction == "forward":
  57. self.part_position.insert(0, [self.part_position[0][0], self.part_position[0][1] - 2 * self.radius])
  58. elif direction == "backward":
  59. self.part_position.insert(0, [self.part_position[0][0], self.part_position[0][1] + 2 * self.radius])
  60. elif direction == "right":
  61. self.part_position.insert(0, [self.part_position[0][0] + 2 * self.radius, self.part_position[0][1]])
  62. elif direction == "left":
  63. self.part_position.insert(0, [self.part_position[0][0] - 2 * self.radius, self.part_position[0][1]])
  64. #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
  65. self.part = self.part_position[-1]
  66. #Xóa khúc cuối
  67. self.part_position.pop(-1)
  68.  
  69. def hit_body(self):
  70. hit = False
  71. for i in range(len(self.part_position) - 1):
  72. if self.part_position[-1] == self.part_position[i]:
  73. hit = True
  74. break
  75. if hit:
  76. return True
  77. else:
  78. return False
  79.  
  80. def hit_wall(self):
  81. if int(self.part_position[0][0] - self.radius) <= 0 or int(self.part_position[0][0] + self.radius) \
  82. >= width or int(self.part_position[0][1] - self.radius) <= 0 \
  83. or int(self.part_position[0][1] + self.radius) >= height:
  84. return True
  85. else:
  86. return False
  87.  
  88. def hit_point(self, point):
  89. if float(point.center_y) - float(point.radius + self.radius) <= self.part_position[0][1] \
  90. <= float(point.center_y) + float(point.radius + self.radius) \
  91. or float(point.center_x) - float(point.radius + self.radius) <= self.part_position[0][0] \
  92. <= float(point.center_x) + float(point.radius + self.radius):
  93. return True
  94. else:
  95. return False
  96.  
  97. def eat_point(self, point):
  98. # Random vị trí của điểm
  99. point.center_x = random.randrange(10, width - 10)
  100. point.center_y = random.randrange(10, height - 10)
  101. # Thêm một khúc vào đuôi nếu ăn điểm. self.part = self.part_position[-1]
  102. self.part_position.insert(-1, self.part)
  103.  
  104.  
  105. class Game:
  106.  
  107. def __init__(self, snake, point, speed):
  108. self.snake = snake
  109. self.point = point
  110. self.speed = speed
  111.  
  112. def draw_arena(self):
  113. # Xóa hết để sau mỗi lần vẽ lại
  114. display_surf.fill((0, 0, 0))
  115. pygame.draw.rect(display_surf, BLACK, (0, 0, width, height), 10)
  116. # Vẽ lại
  117. self.snake.draw()
  118. self.point.draw()
  119.  
  120. def update(self, fps, direction):
  121. #Update các giá trị
  122. if self.snake.hit_point(self.point):
  123. self.snake.eat_point(self.point)
  124. #Tăng speed
  125. fps += self.speed
  126. self.snake.move(direction)
  127. pygame.display.update()
  128. fps_clock.tick(fps)
  129. return fps
  130.  
  131.  
  132. def main():
  133. pygame.init()
  134. snake = Snake([[width / 2, height / 2]], 10)
  135. point = Point(random.randrange(10, width - 10), random.randrange(10, height - 10), 10)
  136. direction = "Right"
  137. game = Game(snake, point, 1)
  138. die = False
  139. fps = 15
  140.  
  141. while True:
  142.  
  143. if snake.hit_body() or snake.hit_wall():
  144. die = True
  145.  
  146. for event in pygame.event.get():
  147. #Dùng phím esc để thoát game, tránh làm máy giật lag
  148. #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
  149. #và tự cắn vào bản thân
  150. if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
  151. die = True
  152. if event.type == KEYDOWN and event.key == K_w and direction != "backward":
  153. direction = "forward"
  154. elif event.type == KEYDOWN and event.key == K_s and direction != "forward":
  155. direction = "backward"
  156. elif event.type == KEYDOWN and event.key == K_a and direction != "right":
  157. direction = "left"
  158. elif event.type == KEYDOWN and event.key == K_d and direction != "left":
  159. direction = "right"
  160.  
  161. if die:
  162. pygame.quit()
  163. sys.exit()
  164. game.draw_arena()
  165. fps = game.update(fps, direction)
  166.  
  167. if __name__ == "__main__":
  168. main()
Add Comment
Please, Sign In to add comment