Advertisement
Guest User

Untitled

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