Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. import pygame,random
  2. from config import Config
  3. def snake(x,y,body_x,body_y,gameDisplay,snakeIMG,bodyIMG):
  4. gameDisplay.blit(snakeIMG, (x,y))
  5. for i in range(len(body_x)):
  6. gameDisplay.blit(bodyIMG, (body_x[i],body_y[i]))
  7. def apple(x,y,gameDisplay,appleIMG):
  8. gameDisplay.blit(appleIMG, (x,y))
  9. def checking(x,y,apple_x,apple_y):
  10. if x == apple_x and y == apple_y:
  11. return True
  12. else:
  13. return False
  14. def end_of_game(x,y,body_x,body_y,display_width,display_height):
  15. if x >= display_width or x < 0 or y >= display_height or y < 0:
  16. print(x,y)
  17. return True
  18. else:
  19. for i in range(len(body_x)):
  20. if body_x[i] == x and body_y[i] ==y:
  21. return True
  22. return False
  23. def size_increasing(x,y,direction,directions,body_x,body_y):
  24. if direction == directions["Right"]:
  25. body_x.append(x-20)
  26. body_y.append(y)
  27. elif direction == directions["Left"]:
  28. body_x.append(x+20)
  29. body_y.append(y)
  30. elif direction == directions["Up"]:
  31. body_x.append(x)
  32. body_y.append(y+20)
  33. elif direction == directions["Down"]:
  34. body_x.append(x)
  35. body_y.append(y-20)
  36. def events(direction, directions):
  37. for event in pygame.event.get():
  38. if event.type == pygame.QUIT:
  39. end = True
  40. if event.type == pygame.KEYDOWN:
  41. if event.key == pygame.K_LEFT and direction != directions["Right"]:
  42. direction = directions["Left"]
  43. elif event.key == pygame.K_RIGHT and direction != directions["Left"]:
  44. direction = directions["Right"]
  45. elif event.key == pygame.K_UP and direction != directions["Down"]:
  46. direction = directions["Up"]
  47. elif event.key == pygame.K_DOWN and direction != directions["Up"]:
  48. direction = directions["Down"]
  49. return direction
  50.  
  51. def moving_snake(body_x, body_y, direction, directions, head_x, head_y):
  52. for i in reversed(range(len(body_x))):
  53. if i !=0:
  54. body_x[i] =body_x[i-1]
  55. body_y[i] =body_y[i-1]
  56. else:
  57. body_x[i] =head_x
  58. body_y[i] =head_y
  59.  
  60. if direction == directions["Right"]:
  61. head_x=head_x+20
  62. elif direction == directions["Left"]:
  63. head_x=head_x-20
  64. elif direction == directions["Up"]:
  65. head_y=head_y-20
  66. elif direction == directions["Down"]:
  67. head_y=head_y+20
  68. return head_x, head_y
  69.  
  70. def main(head_x,head_y,apple_x,apple_y,white,display_width,display_height,body_x,body_y,direction,directions):
  71. pygame.init()
  72. gameDisplay = pygame.display.set_mode((display_width,display_height))
  73. pygame.display.set_caption('Snake')
  74. clock = pygame.time.Clock()
  75. snakeIMG = pygame.image.load('snake.png')
  76. appleIMG = pygame.image.load('apple.png')
  77. bodyIMG = pygame.image.load('body.png')
  78. while not end_of_game(head_x,head_y,body_x,body_y,Config.display_width,Config.display_height):
  79. direction = events(direction, directions)
  80. head_x, head_y = moving_snake(body_x, body_y, direction, directions, head_x, head_y)
  81. gameDisplay.fill(white)
  82. if checking(head_x,head_y,apple_x,apple_y):
  83. apple_x =20 * random.randint(0,39)
  84. apple_y =20 * random.randint(0,29)
  85. apple(apple_x,apple_y,gameDisplay,appleIMG)
  86. size_increasing(head_x,head_y,direction,directions,body_x,body_y)
  87. else:
  88. apple(apple_x,apple_y,gameDisplay,appleIMG)
  89. snake(head_x,head_y,body_x,body_y,gameDisplay,snakeIMG,bodyIMG)
  90. pygame.display.update()
  91. clock.tick(15)
  92.  
  93. pygame.quit()
  94. quit()
  95. main(Config.head_x,Config.head_y,Config.apple_x,Config.apple_y,Config.white,Config.display_width,Config.display_height,Config.body_x,Config.body_y,Config.direction,Config.directions)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement