import random
stage.set_background_color("grey")
stage.set_background_scale(1.5)
#####GLOBAL VARIABLES#####
#the size/diameter of each sprite being drawn
size = 15
#this is the list of coordinate pairs (x,y) for the snake segments
snake = []
#this is the list of sprites representing each segment of the snake
segs = []
#snake x and y movement speeds
snakeDx = 0
snakeDy = -1
#determines how many segments the snake should grow when you eat an apple
growth = 5
#boolean for whether game is running or not
game_over = False
#apple's x and y positions
apple_x = random.randint(-24, 24)
apple_y = random.randint(-24, 24)
#display the score
score = 0
score_display = codesters.Text("Score: 0", 180, 225, "black")
#display the segment count
segment_count = 0
segment_display = codesters.Text("Segments: 0", 160, 200, "black")
#player controls change the dx and dy variables
def up_key():
global snakeDx, snakeDy
snakeDx = 0
snakeDy = 1
stage.event_key("up", up_key)
def down_key():
global snakeDx, snakeDy
snakeDx = 0
snakeDy = -1
#remove_segment()
stage.event_key("down", down_key)
def left_key():
global snakeDx, snakeDy
snakeDx = -1
snakeDy = 0
stage.event_key("left", left_key)
def right_key():
global snakeDx, snakeDy
snakeDx = 1
snakeDy = 0
stage.event_key("right", right_key)
def remove_segment():
global game_over
if(len(segs) == 0):
segment_display.set_text("Segments: 0")
game_over = True
else:
snake.pop(0)
stage.remove_sprite(segs[0])
segs.pop(0)
#calculate and add a new head to the list of snake positions
def add_head(head):
global size
snake_x = head[0] + snakeDx
snake_y = head[1] + snakeDy
snake.append((snake_x, snake_y))
#get the new head of the snake
head = snake[len(snake) - 1]
#draw the new head
circle = codesters.Circle(head[0] * size, head[1] * size, size, "orange")
#add the new head to the list of snake sprites
segs.append(circle)
update_display()
def update_display():
global segment_count, score, segment_display, score_display
#take the length of the list of segments, minus 1, as the segment count
segment_count = len(segs)-1
#update the display on the game board
segment_display.set_text("Segments: " + str(segment_count))
score_display.set_text("Score: " + str(score))
#check if snake is eating apple
def check_apple(head, apple):
global apple_x, apple_y, size, growth, score
if head[0] == apple_x and head[1] == apple_y:
score += 1
#make new randomized coordinates for the apple
apple_x = random.randint(-24, 24)
apple_y = random.randint(-24, 24)
#increase the growth of the snake
growth += 3
#check if the snake should be growing and grow, if not remove the last sprite and last segment position from snake.
def check_grow():
global growth
#check to see if the snake is growing, if so let it grow
if growth > 0:
growth -= 1
#otherwise remove the tail from both lists and remove the sprite from the screen
else:
remove_segment()
#check if the snake has bitten itself and the game should end
def check_state(head):
global game_over
#if you want the player's game to be over when they hit the walls...
#if head[0] > 25 or head[0] < -25 or head[1] > 25 or head[1] < -25:
#game_over = True
#if you want the player to continue to play, even if they hit the walls
if head[0] > 24:
left_key()
if head[0] < -24:
right_key()
if head[1] > 24:
down_key()
if head[1] < -24:
up_key()
#comment this back in if you want the snake to die when it overlaps on itself
#check for snake biting itself
#for seg in snake:
#if seg is not head and head[0] == seg[0] and head[1] == seg[1]:
#game_over = True
#break
#main game function
def main():
#get global variables
global game_over, size, growth
#add first head to the list of snake positions
snake.append((0, 0))
seg = snake[0]
#draw the first head
circle = codesters.Circle(seg[0] * size, seg[1] * size, size, "orange")
#add the first head to the list of snake sprites
segs.append(circle)
#add in the apple sprite
apple = codesters.Circle(apple_x * size, apple_y * size, size, "red")
#main game loop
while game_over is False:
#get the head of the snake
if(len(snake) > 0):
head = snake[len(snake) - 1]
add_head(head)
new_head = snake[len(snake) - 1]
check_apple(new_head, apple)
check_state(new_head)
check_grow()
#remove the previous apple and add the new one
stage.remove_sprite(apple)
apple = codesters.Circle(apple_x * size, apple_y * size, size, "red")
stage.wait(0.1)
#change the background color if the snake is really small
if len(snake) < 2:
stage.set_background_color("yellow")
else:
stage.set_background_color("grey")
#the game has ended here, display the game over screen
text = codesters.Text("Game Over", 0, 0, "red")
#run the main function to run the game
main()