Advertisement
Guest User

snake apples

a guest
Feb 9th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4.  
  5. red = (255,0,0)
  6. darkRed = (200,0,0)
  7. orange = (255,165,0)
  8. gold = (255,215,0)
  9. darkBlue = (0,0,128)
  10. blue = (0,0,255)
  11.  
  12. class Apples():
  13.  
  14. def __init__(self, grid_width, grid_hight, block_size):
  15. self.grid_width = grid_width
  16. self.grid_hight = grid_hight
  17. self.block_size = block_size
  18. self.appleList = []
  19. self.apples()
  20.  
  21. def apples(self, dmd=False):
  22. if len(self.appleList) <= 1 or random.randrange(0,2) == 1 or dmd:
  23. if dmd or len(self.appleList) < 10:
  24. if random.randrange(0,500) == 1:
  25. typ = 3
  26. elif random.randrange(0,30) == 1:
  27. typ = 2
  28. else:
  29. typ = 1
  30. size = 2
  31. randAppleX = random.randrange(0, self.grid_width) * self.block_size
  32. randAppleY = random.randrange(0, self.grid_hight) * self.block_size
  33. self.appleList.append([randAppleX, randAppleY, typ, size])
  34. if random.randrange(0,2) == 1 and not dmd:
  35. self.apples()
  36.  
  37.  
  38. def update(self, disp, snakeX,snakeY):
  39. typ = 0
  40. for item in self.appleList:
  41. if snakeX == item[0] and snakeY == item[1]:
  42. self.appleList.remove(item)
  43. self.apples()
  44. if item[2] == 1:
  45. typ = 1
  46. elif item[2] == 2:
  47. typ = 2
  48. elif item[2] == 3:
  49. typ = 3
  50. loop = 100
  51. while loop > 0:
  52. self.apples(True)
  53. loop -= 1
  54.  
  55. if item[3] < self.block_size:
  56. offSet = (self.block_size - item[3]) / 2
  57. locX = item[0] + offSet
  58. locY = item[1] + offSet
  59. else:
  60. locX = item[0]
  61. locY = item[1]
  62.  
  63. if item[2] == 1:
  64. pygame.draw.rect(disp, red, [locX, locY, item[3], item[3]])
  65. pygame.draw.rect(disp, darkRed, [locX, locY, item[3], item[3]], 1)
  66. elif item[2] == 2:
  67. pygame.draw.rect(disp, gold, [locX, locY, item[3], item[3]])
  68. pygame.draw.rect(disp, orange, [locX, locY, item[3], item[3]], 1)
  69. elif item[2] == 3:
  70. pygame.draw.rect(disp, blue, [locX, locY, item[3], item[3]])
  71. pygame.draw.rect(disp, darkBlue, [locX, locY, item[3], item[3]], 1)
  72.  
  73. return typ
  74.  
  75.  
  76.  
  77. def appleGrow(self):
  78. for item in self.appleList:
  79. if item[3] < self.block_size:
  80. item[3] += 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement