Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. import pygame, sys, time, random
  2. import numpy
  3.  
  4. # Difficulty settings
  5. # Easy -> 10
  6. # Medium -> 25
  7. # Hard -> 40
  8. # Harder -> 60
  9. # Impossible-> 120
  10. difficulty = 25
  11.  
  12. # Window size
  13. frame_size_x = 720
  14. frame_size_y = 480
  15.  
  16. # Checks for errors encountered
  17. check_errors = pygame.init()
  18. # pygame.init() example output -> (6, 0)
  19. # second number in tuple gives number of errors
  20. if check_errors[1] > 0:
  21. print(f'[!] Had {check_errors[1]} errors when initialising game, exiting...')
  22. sys.exit(-1)
  23. else:
  24. print('[+] Game successfully initialised')
  25.  
  26.  
  27. # Initialise game window
  28. pygame.display.set_caption('Snake Eater')
  29. game_window = pygame.display.set_mode((frame_size_x, frame_size_y))
  30.  
  31.  
  32. # Colors (R, G, B)
  33. black = pygame.Color(0, 0, 0)
  34. white = pygame.Color(255, 255, 255)
  35. red = pygame.Color(255, 0, 0)
  36. green = pygame.Color(0, 255, 0)
  37. blue = pygame.Color(0, 0, 255)
  38.  
  39.  
  40. # FPS (frames per second) controller
  41. fps_controller = pygame.time.Clock()
  42.  
  43.  
  44. # Game variables
  45. snake_pos = [100, 50]
  46. snake_body = [[100, 50], [100-10, 50], [100-(2*10), 50]]
  47.  
  48. food_pos = [random.randrange(1, (frame_size_x//10)) * 10, random.randrange(1, (frame_size_y//10)) * 10]
  49. food_spawn = True
  50.  
  51. direction = 'RIGHT'
  52. change_to = direction
  53.  
  54. score = 0
  55.  
  56.  
  57. # Game Over
  58. def game_over():
  59. my_font = pygame.font.SysFont('times new roman', 90)
  60. game_over_surface = my_font.render('THE END', True, red)
  61. game_over_rect = game_over_surface.get_rect()
  62. game_over_rect.midtop = (frame_size_x/2, frame_size_y/4)
  63. game_window.fill(black)
  64. game_window.blit(game_over_surface, game_over_rect)
  65. show_score(0, red, 'times', 20)
  66. pygame.display.flip()
  67. time.sleep(3)
  68. pygame.quit()
  69. sys.exit()
  70.  
  71.  
  72. # Score
  73. def show_score(choice, color, font, size):
  74. score_font = pygame.font.SysFont(font, size)
  75. score_surface = score_font.render('Score : ' + str(score), True, color)
  76. score_rect = score_surface.get_rect()
  77. if choice == 1:
  78. score_rect.midtop = (frame_size_x/10, 15)
  79. else:
  80. score_rect.midtop = (frame_size_x/2, frame_size_y/1.25)
  81. game_window.blit(score_surface, score_rect)
  82. # pygame.display.flip()
  83.  
  84.  
  85. arr=numpy.arange(100)
  86.  
  87. # Main logic
  88. while True:
  89. f=open("TXT\\prueba.txt","r")
  90. valor=f.readline()
  91. valor = valor.strip()
  92. # Whenever a key is pressed down
  93. while valor:
  94. print(valor)
  95. #nn return activation function value
  96. #valor={-0.75,-0.25,0.25,0.75}
  97. # W -> Up; S -> Down; A -> Left; D -> Right
  98. if valor=="0":
  99. #change_to=-0.75
  100. change_to = 'UP'
  101. if valor=="1":
  102. #change_to=-0.25
  103. change_to = 'DOWN'
  104. if valor=="2":
  105. #change_to=0.25
  106. change_to = 'LEFT'
  107. if valor=="3":
  108. #change_to=0.75
  109. change_to = 'RIGHT'
  110. # Esc -> Create event to quit the game
  111.  
  112. # Making sure the snake cannot move in the opposite direction instantaneously
  113. if change_to == 'UP' and direction != 'DOWN':
  114. direction = 'UP'
  115. if change_to == 'DOWN' and direction != 'UP':
  116. direction = 'DOWN'
  117. if change_to == 'LEFT' and direction != 'RIGHT':
  118. direction = 'LEFT'
  119. if change_to == 'RIGHT' and direction != 'LEFT':
  120. direction = 'RIGHT'
  121.  
  122. # Moving the snake
  123. if direction == 'UP':
  124. snake_pos[1] -= 10
  125. if direction == 'DOWN':
  126. snake_pos[1] += 10
  127. if direction == 'LEFT':
  128. snake_pos[0] -= 10
  129. if direction == 'RIGHT':
  130. snake_pos[0] += 10
  131.  
  132. # Snake body growing mechanism
  133. snake_body.insert(0, list(snake_pos))
  134. if snake_pos[0] == food_pos[0] and snake_pos[1] == food_pos[1]:
  135. score += 1
  136. food_spawn = False
  137. else:
  138. snake_body.pop()
  139.  
  140. # Spawning food on the screen
  141. if not food_spawn:
  142. food_pos = [random.randrange(1, (frame_size_x//10)) * 10, random.randrange(1, (frame_size_y//10)) * 10]
  143. food_spawn = True
  144.  
  145. # GFX
  146. game_window.fill(black)
  147. for pos in snake_body:
  148. # Snake body
  149. # .draw.rect(play_surface, color, xy-coordinate)
  150. # xy-coordinate -> .Rect(x, y, size_x, size_y)
  151. pygame.draw.rect(game_window, green, pygame.Rect(pos[0], pos[1], 10, 10))
  152.  
  153. # Snake food
  154. pygame.draw.rect(game_window, white, pygame.Rect(food_pos[0], food_pos[1], 10, 10))
  155.  
  156. # Game Over conditions
  157. # Getting out of bounds
  158. if snake_pos[0] < 0 or snake_pos[0] > frame_size_x-10:
  159. game_over()
  160. if snake_pos[1] < 0 or snake_pos[1] > frame_size_y-10:
  161. game_over()
  162. # Touching the snake body
  163. for block in snake_body[1:]:
  164. if snake_pos[0] == block[0] and snake_pos[1] == block[1]:
  165. game_over()
  166.  
  167. show_score(1, white, 'consolas', 20)
  168. # Refresh game screen
  169. pygame.display.update()
  170. # Refresh rate
  171. fps_controller.tick(difficulty)
  172. valor=f.readline()
  173. valor = valor.strip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement