Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # File Name: snakeActual.py
- # Description: Base code from snake_template.py with edits to optimize
- # Author: ICS2O (edited by Jayden Chuong)
- # Date: 12/04/2017 -
- from random import randint
- import math
- import pygame
- pygame.init()
- WIDTH = 800
- HEIGHT= 600
- MIDDLE = int(WIDTH/2.0)
- TOP = 0
- BOTTOM = HEIGHT
- screen=pygame.display.set_mode((WIDTH,HEIGHT))
- WHITE = (255,255,255)
- LIGREY = (190,190,190)
- GREY = (150,150,150)
- BLACK = ( 0, 0, 0)
- RED = (255, 50, 50)
- GREEN = ( 0,255, 50)
- CYAN = ( 0,100,100)
- BLUE = ( 0, 50,255)
- outline = 0
- delay = 50
- picture = pygame.image.load("ainsley.png")
- # snake's properties
- BODY_SIZE = 10
- HSTEP = 20
- VSTEP = 20
- speedX = 0
- speedY = -VSTEP # initially the snake moves from bottom up
- segX = []
- segY = []
- lastKey = "u"
- speedIncrease1 = True
- speedIncrease2 = True
- score = 0
- # apple's properties
- spawnX1 = 2
- spawnY1 = 2
- spawnX2 = 38
- spawnY2 = 28
- appleX = [randint(spawnX1,spawnX2)*20]
- appleY = [randint(spawnY1,spawnY2)*20]
- font = pygame.font.SysFont("Century Gothic",36, bold = True)
- # time properties
- BEGIN = pygame.time.get_ticks()
- elapsed = 0
- clock = pygame.time.Clock()
- FPS = 24
- # function that redraws all objects
- def redrawScreen(score):
- screen.fill(BLACK)
- screen.blit(picture, (280,200))
- for i in range(len(segX)):
- pygame.draw.circle(screen, GREEN, (segX[0], segY[0]), BODY_SIZE, outline)
- pygame.draw.circle(screen, BLUE, (segX[i], segY[i]), BODY_SIZE, outline)
- for i in range(len(appleX)):
- if segX[0] == appleX[i] and segY[0] == appleY[i]:
- appleX.pop(0)
- appleY.pop(0)
- appleX.append(randint(spawnX1,spawnX2)*20)
- appleY.append(randint(spawnY1,spawnY2)*20)
- pygame.draw.circle(screen, RED, (appleX[i], appleY[i]), BODY_SIZE, outline)
- score = font.render(str(score), 1, WHITE)
- time = font.render(str(elapsed/1000.0), 1, WHITE)
- screen.blit(score, (30,30))
- screen.blit(time, (680,30))
- pygame.display.update()
- # the main program begins here
- for i in range(4): # add coordinates for the head and 3 segments
- segX.append(MIDDLE)
- segY.append(BOTTOM - 40 + i*VSTEP)
- print "Use the arrows and the space bar."
- print "Hit ESC to end the program."
- inPlay = True
- score = 0
- while inPlay:
- redrawScreen(score)
- clock.tick(FPS)
- pygame.time.delay(delay)
- elapsed = pygame.time.get_ticks() - BEGIN
- # check for events
- pygame.event.get()
- keys = pygame.key.get_pressed()
- if keys[pygame.K_ESCAPE]:
- inPlay = False
- if lastKey != "r":
- if keys[pygame.K_LEFT]:
- speedX = HSTEP
- speedY = 0
- lastKey = "l"
- if lastKey != "l":
- if keys[pygame.K_RIGHT]:
- speedX = -HSTEP
- speedY = 0
- lastKey = "r"
- if lastKey != "d":
- if keys[pygame.K_UP]:
- speedX = 0
- speedY = VSTEP
- lastKey = "u"
- if lastKey != "u":
- if keys[pygame.K_DOWN]:
- speedX = 0
- speedY = -VSTEP
- lastKey = "d"
- # move the segments
- numOfSegments = len(segX)-1
- for i in range(numOfSegments,0,-1):
- if segX[i] != segX[0] + speedX or segY[i] != segY[0] + speedY: # starting from the tail, and going backwards:
- segX[i]=segX[i-1] # every segment takes the coordinates
- segY[i]=segY[i-1] # of the previous one
- else:
- inPlay = False
- # move the head
- segX[0] = segX[0] + speedX
- segY[0] = segY[0] + speedY
- # game over if escape
- if segX[0] >= WIDTH or segX[0] <= 0 or segY[0] >= HEIGHT or segY[0] <= 0:
- inPlay = False
- # if apple is eaten by snake
- for i in range(len(appleX)):
- if segX[0] == appleX[i] and segY[0] == appleY[i]:
- ## segX.append(segX[-1])
- ## segY.append(segY[-1])
- BODY_SIZE = BODY_SIZE+5
- score = score + 1
- # increase speed if certain amount eaten
- if score == 10:
- if speedIncrease1 == True:
- delay = delay - 10
- speedIncrease1 = False
- if score == 25:
- spawnX1 = 1
- spawnY1 = 1
- spawnX2 = 39
- spawnY2 = 29
- if speedIncrease2 == True:
- delay = delay - 10
- speedIncrease2 = False
- #---------------------------------------#
- print "Time:",str(round(elapsed/1000.0,1)),"seconds"
- print "GAME OVER"
- print "Your score was:",score
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement