Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1. import pygame, sys, random, math, time
  2. from pygame.locals import *
  3. #defines some key variables
  4. cell_x_coord = list()
  5. cell_y_coord = list()
  6. x_coord = 0
  7. y_coord = 0
  8. counter = 0
  9. left = bool()
  10. right = bool()
  11. upwards = bool()
  12. downwards = bool()
  13. x_coord_for_food = list()
  14. y_coord_for_food = list()
  15. x_coord_list = list()
  16. y_coord_list = list()
  17.  
  18. pygame.init()
  19. #defining variables for the width and length of screen
  20. LENGTH = 500
  21. WIDTH = 500
  22. FPS = 1
  23. #this makes the screen appear
  24. DISPLAYSURF = pygame.display.set_mode((WIDTH,LENGTH), 0, 32)
  25. #the class to split up the screen (refer to line 84 for more information)
  26. class split_up:
  27. def __init__(self):
  28. t = 10
  29. WIDTH = 490
  30. self.width_split_list = []
  31. WIDTH = int(WIDTH/10)
  32. for i in range(WIDTH):
  33. self.width_split_list.append(t)
  34. t+=10
  35. self.length_split_list = []
  36. j = 10
  37. LENGTH = 490
  38. LENGTH = int(LENGTH/10)
  39. for i in range((LENGTH)):
  40. self.length_split_list.append(j)
  41. j+=10
  42. return
  43. #defines the csolors
  44. WHITE = (255,255,255)
  45. RED = (255,0,0)
  46. BLACK = (0,0,0)
  47. FOODCOLOR = (180, 159, 7)
  48. #pygame.draw.rect(foo) is complicated and ineficiant, therefore this function creates the snake
  49. def draw_square_snake(area,x_coord,y_coord,COLOR):
  50. area = math.sqrt(area)
  51. pygame.draw.rect(DISPLAYSURF, COLOR, (x_coord, y_coord, area, area))
  52. return
  53. #this function draws black for the ares where the snake used to be, and the ares where the snake ate the food
  54. def draw_square_background(area, x_coord, y_coord, COLOR):
  55. area = math.sqrt(area)
  56. pygame.draw.rect(DISPLAYSURF, COLOR, (x_coord, y_coord, area , area))
  57. return
  58. #this class makes the food
  59. class make_food:
  60. def __init__(self, area,COLOR):
  61. self.color = COLOR
  62. self.area = math.sqrt(area)
  63. split_up_object = split_up()
  64. self.random_width = random.choice(split_up_object.width_split_list)
  65. self.random_length = random.choice(split_up_object.length_split_list)
  66. pygame.draw.rect(DISPLAYSURF, self.color, (self.random_width, self.random_length, 10,10))
  67. return
  68. def main():
  69. #defining varaibles for the loop
  70. area = 100
  71. clock = pygame.time.Clock()
  72. count = 0
  73. counter = 0
  74. left = bool()
  75. right = bool()
  76. upwards = bool()
  77. downwards = bool()
  78. x_coord = 20
  79. y_coord = 20
  80. eat_counter = 0
  81. eaten = bool()
  82. cell_x_coord = list()
  83. cell_y_coord = list()
  84. speed = 10
  85. var = 10
  86. food_eaten = bool()
  87. cell_count = 0
  88. #splits up the screen into squares of 10 (area = math.sqrt(WIDTH)/ math.sqrt(LENGTH))
  89. split_up_object = split_up()
  90. random_width = random.choice(split_up_object.width_split_list)
  91. random_length = random.choice(split_up_object.length_split_list)
  92. timer_event = pygame.USEREVENT+1
  93. pygame.time.set_timer(timer_event, 250)
  94. while True:
  95.  
  96. #these 'try' statements will do things, if it is the first iteration of the loop, it will pass
  97. try:
  98. draw_square_background(500, cell_x_coord[0], cell_y_coord[0], BLACK)
  99. except IndexError:
  100. time.sleep(0.0000000000000001)
  101. try:
  102. del cell_x_coord[0]
  103. del cell_y_coord[0]
  104. except IndexError:
  105. time.sleep(0.0000000000000001)
  106.  
  107. #this will set the FPS to 30
  108. clock.tick(30)
  109.  
  110. #the only things that will happen in this event handler is if the user wants to quit, the program will end
  111. for event in pygame.event.get():
  112. if event.type == QUIT:
  113. pygame.quit()
  114. sys.exit()
  115. pygame.display.update()
  116.  
  117. #these will check if the user has pressed any of the arrow keys, any other event will be sent to the event handler, (above this comment) and since the only algorithm in that is the QUIT, nothing will happen
  118. if pygame.key.get_pressed()[pygame.K_UP] == True:
  119. upwards = True
  120. downwards = False
  121. left = False
  122. right = False
  123. elif pygame.key.get_pressed()[pygame.K_DOWN] == True:
  124. downwards = True
  125. upwards = False
  126. left = False
  127. right = False
  128. elif pygame.key.get_pressed()[pygame.K_LEFT] == True:
  129. left = True
  130. upwards = False
  131. downwards = False
  132. right = False
  133. elif pygame.key.get_pressed()[pygame.K_RIGHT] == True:
  134.  
  135. right = True
  136. left = False
  137. downwards = False
  138. upwards = False
  139. #passes if none of the arrow keys are pressed
  140. else:
  141. pass
  142. #the reason why the x_coord and y_coord don't go higher in the first statements is because the program needs to continue even if no keys are pressed, therefore they have their own seperate statements
  143. if upwards == False and downwards == False and right == False and left == False:
  144. x_coord+=0
  145. y_coord+=0
  146. if upwards == True:
  147. x_coord+=0
  148. y_coord-=speed
  149. elif downwards == True:
  150. x_coord+=0
  151. y_coord+=speed
  152. elif right == True:
  153. x_coord+=speed
  154. y_coord+=0
  155. elif left == True:
  156. x_coord-=speed
  157. y_coord+=0
  158. if x_coord >=int((WIDTH-19)):
  159. pygame.quit()
  160. sys.exit()
  161. elif x_coord <=-2:
  162. pygame.quit()
  163. sys.exit()
  164. elif y_coord >=int((LENGTH-19)):
  165. pygame.quit()
  166. sys.exit()
  167. elif y_coord<=-2:
  168. pygame.quit()
  169. sys.exit()
  170. #although this is only 2 lines, it could be the most important part of the program
  171. cell_x_coord.append(x_coord)
  172. cell_y_coord.append(y_coord)
  173. #draws the food if the loop has only been through one iteration
  174. if eat_counter == 0:
  175. area = math.sqrt(area)
  176. split_up_object = split_up()
  177. random_width = random.choice(split_up_object.width_split_list)
  178. random_length = random.choice(split_up_object.length_split_list)
  179. pygame.draw.rect(DISPLAYSURF, FOODCOLOR, (20, 20, 1,1))
  180. food_for_snake = make_food(10, FOODCOLOR)
  181. eat_counter+=1
  182. #length_x_coord=len(x_coord)
  183. #checks if the head of the snake is in the range of the food
  184. #print(len(x_coord))
  185. #print(len(cell_x_coord))
  186. if (((cell_x_coord[0] <= food_for_snake.random_width and cell_x_coord[0] >= (food_for_snake.random_width-var)) and (cell_y_coord[0] <= food_for_snake.random_length and cell_y_coord[0] >= (food_for_snake.random_length-var))) or ((cell_y_coord[0] <=food_for_snake.random_length and cell_y_coord[0] >= (food_for_snake.random_length-var)) and cell_x_coord[0] <= food_for_snake.random_width and cell_x_coord[0] >= (food_for_snake.random_width-var)) and food_eaten !=True):
  187. food_eaten = True
  188. last_in_list_x = int((len(cell_x_coord)-1))
  189. last_in_list_y = int((len(cell_y_coord)-1))
  190. last_element_x = cell_x_coord[last_in_list_x]
  191. last_element_y = cell_y_coord[last_in_list_y]
  192. for i in range((len(cell_x_coord))):
  193. draw_square_snake(500, cell_x_coord[i], cell_y_coord[i], RED)
  194. if food_eaten == 1:
  195. cell_x_coord.append((x_coord-1000))
  196. cell_y_coord.append((y_coord))
  197. food_for_snake = make_food(10, FOODCOLOR)
  198. food_eaten = False
  199. #print(cell_count)
  200. #cell_count+=1
  201. pygame.display.update()
  202. if __name__== '__main__':
  203. #executes the 'main' function
  204. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement