We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pip install pygame import pygame import time import random
pygame.init()
WIDTH, HEIGHT = 600, 400
WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (213, 50, 80) GREEN = (0, 255, 0) BLUE = (50, 153, 213)
screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Snake Game")
clock = pygame.time.Clock() snake_block = 10 snake_speed = 15 font_style = pygame.font.SysFont("bahnschrift", 25) score_font = pygame.font.SysFont("comicsansms", 35)
def display_score(score): value = score_font.render("Score: " + str(score), True, GREEN) screen.blit(value, [10, 10])
def draw_snake(block, snake_list): for x in snake_list: pygame.draw.rect(screen, BLUE, [x[0], x[1], block, block])
def message(msg, color): msg_surface = font_style.render(msg, True, color) screen.blit(msg_surface, [WIDTH / 6, HEIGHT / 3])
def game_loop(): game_over = False game_close = False
x1, y1 = WIDTH // 2, HEIGHT // 2 x1_change, y1_change = 0, 0 snake_list = [] length_of_snake = 1 food_x = round(random.randrange(0, WIDTH - snake_block) / 10.0) * 10.0 food_y = round(random.randrange(0, HEIGHT - snake_block) / 10.0) * 10.0 while not game_over: while game_close: screen.fill(WHITE) message("You lost! Press Q-Quit or C-Play Again", RED) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: game_over = True game_close = False if event.key == pygame.K_c: game_loop() for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x1_change = -snake_block y1_change = 0 elif event.key == pygame.K_RIGHT: x1_change = snake_block y1_change = 0 elif event.key == pygame.K_UP: y1_change = -snake_block x1_change = 0 elif event.key == pygame.K_DOWN: y1_change = snake_block x1_change = 0 if x1 >= WIDTH or x1 < 0 or y1 >= HEIGHT or y1 < 0: game_close = True x1 += x1_change y1 += y1_change screen.fill(BLACK) pygame.draw.rect(screen, GREEN, [food_x, food_y, snake_block, snake_block]) snake_head = [x1, y1] snake_list.append(snake_head) if len(snake_list) > length_of_snake: del snake_list[0] for block in snake_list[:-1]: if block == snake_head: game_close = True draw_snake(snake_block, snake_list) display_score(length_of_snake - 1) pygame.display.update() if x1 == food_x and y1 == food_y: food_x = round(random.randrange(0, WIDTH - snake_block) / 10.0) * 10.0 food_y = round(random.randrange(0, HEIGHT - snake_block) / 10.0) * 10.0 length_of_snake += 1 clock.tick(snake_speed) pygame.quit() quit()
game_loop()
The text was updated successfully, but these errors were encountered:
No branches or pull requests
pip install pygame
import pygame
import time
import random
Initialize Pygame
pygame.init()
Screen dimensions
WIDTH, HEIGHT = 600, 400
Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (213, 50, 80)
GREEN = (0, 255, 0)
BLUE = (50, 153, 213)
Create screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game")
Clock and font
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 15
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
def display_score(score):
value = score_font.render("Score: " + str(score), True, GREEN)
screen.blit(value, [10, 10])
def draw_snake(block, snake_list):
for x in snake_list:
pygame.draw.rect(screen, BLUE, [x[0], x[1], block, block])
def message(msg, color):
msg_surface = font_style.render(msg, True, color)
screen.blit(msg_surface, [WIDTH / 6, HEIGHT / 3])
def game_loop():
game_over = False
game_close = False
game_loop()
The text was updated successfully, but these errors were encountered: