daily pastebin goal
9%
SHARE
TWEET

jxyden

a guest Dec 7th, 2017 41 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # File Name: snakeActual.py
  2. # Description: Base code from snake_template.py with edits to optimize
  3. # Author: ICS2O (edited by Jayden Chuong)
  4. # Date: 12/04/2017 -
  5.  
  6. from random import randint
  7. import math
  8. import pygame
  9. pygame.init()
  10. WIDTH = 800
  11. HEIGHT= 600
  12. MIDDLE = int(WIDTH/2.0)
  13. TOP = 0
  14. BOTTOM = HEIGHT
  15. screen=pygame.display.set_mode((WIDTH,HEIGHT))
  16.  
  17. WHITE = (255,255,255)
  18. LIGREY = (190,190,190)
  19. GREY = (150,150,150)
  20. BLACK = (  0,  0,  0)
  21. RED = (255, 50, 50)
  22. GREEN = (  0,255, 50)
  23. CYAN = (  0,100,100)
  24. BLUE = (  0, 50,255)
  25. outline = 0
  26. delay = 50
  27. picture = pygame.image.load("ainsley.png")
  28.  
  29. # snake's properties
  30. BODY_SIZE = 10
  31. HSTEP = 20
  32. VSTEP = 20
  33. speedX = 0
  34. speedY = -VSTEP                         # initially the snake moves from bottom up
  35. segX = []
  36. segY = []
  37. lastKey = "u"
  38. speedIncrease1 = True
  39. speedIncrease2 = True
  40. score = 0
  41.  
  42. # apple's properties
  43. spawnX1 = 2
  44. spawnY1 = 2
  45. spawnX2 = 38
  46. spawnY2 = 28
  47. appleX = [randint(spawnX1,spawnX2)*20]
  48. appleY = [randint(spawnY1,spawnY2)*20]
  49. font = pygame.font.SysFont("Century Gothic",36, bold = True)
  50.  
  51. # time properties
  52. BEGIN = pygame.time.get_ticks()
  53. elapsed = 0
  54. clock = pygame.time.Clock()
  55. FPS = 24
  56.  
  57. # function that redraws all objects
  58. def redrawScreen(score):
  59.     screen.fill(BLACK)
  60.     screen.blit(picture, (280,200))
  61.     for i in range(len(segX)):
  62.         pygame.draw.circle(screen, GREEN, (segX[0], segY[0]), BODY_SIZE, outline)
  63.         pygame.draw.circle(screen, BLUE, (segX[i], segY[i]), BODY_SIZE, outline)
  64.         for i in range(len(appleX)):
  65.             if segX[0] == appleX[i] and segY[0] == appleY[i]:
  66.                 appleX.pop(0)
  67.                 appleY.pop(0)
  68.                 appleX.append(randint(spawnX1,spawnX2)*20)
  69.                 appleY.append(randint(spawnY1,spawnY2)*20)
  70.             pygame.draw.circle(screen, RED, (appleX[i], appleY[i]), BODY_SIZE, outline)
  71.     score = font.render(str(score), 1, WHITE)
  72.     time = font.render(str(elapsed/1000.0), 1, WHITE)
  73.     screen.blit(score, (30,30))
  74.     screen.blit(time, (680,30))
  75.     pygame.display.update()
  76.    
  77.    
  78. # the main program begins here
  79. for i in range(4):                      # add coordinates for the head and 3 segments
  80.     segX.append(MIDDLE)
  81.     segY.append(BOTTOM - 40 + i*VSTEP)
  82.  
  83. print "Use the arrows and the space bar."
  84. print "Hit ESC to end the program."    
  85.  
  86. inPlay = True
  87. score = 0
  88. while inPlay:
  89.     redrawScreen(score)
  90.     clock.tick(FPS)
  91.     pygame.time.delay(delay)
  92.  
  93.     elapsed = pygame.time.get_ticks() - BEGIN
  94.    
  95. # check for events
  96.     pygame.event.get()
  97.     keys = pygame.key.get_pressed()
  98.     if keys[pygame.K_ESCAPE]:
  99.         inPlay = False
  100.     if lastKey != "r":
  101.         if keys[pygame.K_LEFT]:
  102.             speedX = HSTEP
  103.             speedY = 0
  104.             lastKey = "l"
  105.     if lastKey != "l":
  106.         if keys[pygame.K_RIGHT]:
  107.             speedX = -HSTEP
  108.             speedY = 0
  109.             lastKey = "r"
  110.     if lastKey != "d":
  111.         if keys[pygame.K_UP]:
  112.             speedX = 0
  113.             speedY = VSTEP
  114.             lastKey = "u"
  115.     if lastKey != "u":
  116.         if keys[pygame.K_DOWN]:
  117.             speedX = 0
  118.             speedY = -VSTEP
  119.             lastKey = "d"
  120.  
  121. # move the segments
  122.     numOfSegments = len(segX)-1
  123.     for i in range(numOfSegments,0,-1):
  124.         if segX[i] != segX[0] + speedX or segY[i] != segY[0] + speedY:           # starting from the tail, and going backwards:
  125.             segX[i]=segX[i-1]               # every segment takes the coordinates
  126.             segY[i]=segY[i-1]               # of the previous one
  127.         else:
  128.             inPlay = False
  129.  
  130. # move the head
  131.     segX[0] = segX[0] + speedX
  132.     segY[0] = segY[0] + speedY
  133.  
  134. # game over if escape
  135.     if segX[0] >= WIDTH or segX[0] <= 0 or segY[0] >= HEIGHT or segY[0] <= 0:
  136.         inPlay = False
  137.  
  138. # if apple is eaten by snake
  139.     for i in range(len(appleX)):
  140.         if segX[0] == appleX[i] and segY[0] == appleY[i]:
  141. ##            segX.append(segX[-1])
  142. ##            segY.append(segY[-1])
  143.             BODY_SIZE = BODY_SIZE+5
  144.             score = score + 1
  145.            
  146.  
  147. # increase speed if certain amount eaten
  148.     if score == 10:
  149.         if speedIncrease1 == True:
  150.             delay = delay - 10
  151.             speedIncrease1 = False
  152.     if score == 25:
  153.         spawnX1 = 1
  154.         spawnY1 = 1
  155.         spawnX2 = 39
  156.         spawnY2 = 29
  157.         if speedIncrease2 == True:
  158.             delay = delay - 10
  159.             speedIncrease2 = False
  160.  
  161. #---------------------------------------#
  162. print "Time:",str(round(elapsed/1000.0,1)),"seconds"
  163. print "GAME OVER"
  164. print "Your score was:",score
  165. pygame.quit()
RAW Paste Data
Top