Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import time
- import random
- snake_speed = 15
- height = 720
- width = 480
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- GREEN = (0, 255, 0)
- BLUE = (0, 0, 255)
- pygame.init()
- game_window = pygame.display.set_mode((width, height))
- pygame.display.set_caption("Snake")
- fps = pygame.time.Clock() #frames per second = fps
- snake_position = [100, 50]
- #defining first 4 blocks of snake body
- snake_body = [[100, 50],
- [90, 50],
- [80, 50],
- [70, 50]
- ]
- fruit_position = [random.randrange(1, (width//10)) * 10,
- random.randrange(1, (height//10)) * 10]
- fruit_spawn = True
- direction = 'RIGHT'
- change_to = direction
- #initial score
- score = 0
- #function to display score
- def show_score(choice, color, font, size):
- score_font = pygame.font.SysFont(font, size)
- score_surface = score_font.render('Score: ' + str(score), True, color)
- #creating a rectangular object for the text
- score_rect = score_surface.get_rect()
- #displaying text
- game_window.blit(score_surface, score_rect)
- #game over function
- def game_over():
- my_font = pygame.font.SysFont('times new roman', 50)
- #defining surface on which to draw text
- game_over_surface = my_font.render('Your score is:' + str(score), True, RED)
- #creating a rectangular object for the text
- game_over_rect = game_over_surface.get_rect()
- #setting potision of the text
- game_over_rect.midtop = (width/2, height/4)
- game_window.blit(game_over_surface, game_over_rect)
- pygame.display.flip()
- #adding line to quit program after 2 seconds
- time.sleep(2) #value is time in seconds
- #deactivating pygame library
- pygame.quit()
- #qutting the program
- quit()
- #main
- while True:
- for event in pygame.event.get():
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_UP:
- change_to = 'UP'
- if event.key == pygame.K_DOWN:
- change_to = 'DOWN'
- if event.key == pygame.K_LEFT:
- change_to = 'LEFT'
- if event.key == pygame.K_RIGHT:
- change_to == 'RIGHT'
- #if two keys pressed simultaneously, we dont want
- #snake to move in two different directions
- if change_to == 'UP' and direction != 'DOWN':
- direction = 'UP'
- if change_to == 'DOWN' and direction != 'UP':
- direction = 'DOWN'
- if change_to == 'LEFT' and direction != 'RIGHT':
- direction = 'LEFT'
- if change_to == 'RIGHT' and direction != 'LEFT':
- direction = 'RIGHT'
- #moving the snake
- if direction == 'UP':
- snake_position[1] -= 10
- if direction == 'DOWN':
- snake_position[1] += 10
- if direction == 'LEFT':
- snake_position[0] -= 10
- if direction == 'RIGHT':
- snake_position[0] += 10
- #snake body growing mechanism
- #if fruit and snake collide then scores increase by 1
- snake_body.insert(0, list(snake_position))
- if snake_position[0] == fruit_position[0] and snake_position[1] == fruit_position[1]:
- score += 1
- fruit_spawn = False
- else:
- snake_body.pop()
- if not fruit_spawn:
- fruit_position = [random.randrange(1, (width//10)) * 10,
- random.randrange(1, (height//10)) * 10]
- fruit_spawn = True
- game_window.fill(BLACK)
- for pos in snake_body:
- pygame.draw.rect(game_window, GREEN, pygame.Rect(pos[0], pos[1], 10, 10))
- pygame.draw.rect(game_window, WHITE, pygame.Rect(fruit_position[0], fruit_position[1], 10, 10))
- #game over condition
- if snake_position[0] < 0 or snake_position[0] > width-10:
- game_over()
- if snake_position[1] < 0 or snake_position[1] > height-10:
- game_over()
- #touching snake body
- for block in snake_body[1:]:
- if snake_position[0] == block[0] and snake_position[1] == block[1]:
- game_over()
- #displaying score continuously
- show_score(1, WHITE, 'times new roman', 20)
- #updating window
- pygame.display.update()
- #refresh rate/frame rate per second
- fps.tick(snake_speed)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement