karen_constantine

Untitled

Aug 26th, 2023
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. import pygame
  2. import time
  3. import random
  4.  
  5. pygame.init()
  6.  
  7. white = (255, 255, 255)
  8. yellow = (255, 255, 102)
  9. black = (0, 0, 0)
  10. red = (213, 50, 80)
  11. green = (0, 255, 0)
  12. blue = (50, 153, 213)
  13.  
  14. dis_width = 600
  15. dis_height = 400
  16.  
  17. dis = pygame.display.set_mode((dis_width, dis_height))
  18. pygame.display.set_caption('Snake Game by Andrew Abramoff')
  19.  
  20. clock = pygame.time.Clock()
  21.  
  22. snake_block = 10
  23. snake_speed = 15
  24.  
  25. font_style = pygame.font.SysFont("bahnschrift", 25)
  26. score_font = pygame.font.SysFont("comicsansms", 35)
  27.  
  28.  
  29. def Your_score(score):
  30. value = score_font.render("Your Score: " + str(score), True, yellow)
  31. dis.blit(value, [0, 0])
  32.  
  33.  
  34.  
  35. def our_snake(snake_block, snake_list):
  36. for x in snake_list:
  37. pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
  38.  
  39.  
  40. def message(msg, color):
  41. mesg = font_style.render(msg, True, color)
  42. dis.blit(mesg, [dis_width / 6, dis_height / 3])
  43.  
  44.  
  45. def gameLoop():
  46. game_over = False
  47. game_close = False
  48.  
  49. x1 = dis_width / 2
  50. y1 = dis_height / 2
  51.  
  52. x1_change = 0
  53. y1_change = 0
  54.  
  55. snake_List = []
  56. Length_of_snake = 1
  57.  
  58. foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
  59. foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
  60.  
  61. while not game_over:
  62.  
  63. while game_close == True:
  64. dis.fill(blue)
  65. message("You Lost! Press C-Play Again or Q-Quit", red)
  66. Your_score(Length_of_snake - 1)
  67. pygame.display.update()
  68.  
  69. for event in pygame.event.get():
  70. if event.type == pygame.KEYDOWN:
  71. if event.key == pygame.K_q:
  72. game_over = True
  73. game_close = False
  74. if event.key == pygame.K_c:
  75. gameLoop()
  76.  
  77. for event in pygame.event.get():
  78. if event.type == pygame.QUIT:
  79. game_over = True
  80. if event.type == pygame.KEYDOWN:
  81. if event.key == pygame.K_LEFT:
  82. x1_change = -snake_block
  83. y1_change = 0
  84. elif event.key == pygame.K_RIGHT:
  85. x1_change = snake_block
  86. y1_change = 0
  87. elif event.key == pygame.K_UP:
  88. y1_change = -snake_block
  89. x1_change = 0
  90. elif event.key == pygame.K_DOWN:
  91. y1_change = snake_block
  92. x1_change = 0
  93.  
  94. if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
  95. game_close = True
  96. x1 += x1_change
  97. y1 += y1_change
  98. dis.fill(blue)
  99. pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block])
  100. snake_Head = []
  101. snake_Head.append(x1)
  102. snake_Head.append(y1)
  103. snake_List.append(snake_Head)
  104. if len(snake_List) > Length_of_snake:
  105. del snake_List[0]
  106.  
  107. for x in snake_List[:-1]:
  108. if x == snake_Head:
  109. game_close = True
  110.  
  111. our_snake(snake_block, snake_List)
  112. Your_score(Length_of_snake - 1)
  113.  
  114. pygame.display.update()
  115.  
  116. if x1 == foodx and y1 == foody:
  117. foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
  118. foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
  119. Length_of_snake += 1
  120.  
  121. clock.tick(snake_speed)
  122.  
  123. pygame.quit()
  124. quit()
  125.  
  126.  
  127. gameLoop()
Advertisement
Add Comment
Please, Sign In to add comment