Skip to content
New issue

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

Snake.go #4174

Open
Crafter0987 opened this issue Mar 10, 2024 · 2 comments
Open

Snake.go #4174

Crafter0987 opened this issue Mar 10, 2024 · 2 comments

Comments

@Crafter0987
Copy link

import pygame
import random

Initialize Pygame

pygame.init()

Set up display

width, height = 800, 600
display = pygame.display.set_mode((width, height))
pygame.display.set_caption("Snake Game")

Colors

white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)

Snake attributes

snake_block = 20
snake_speed = 15
font_style = pygame.font.SysFont(None, 50)

Snake function

def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(display, green, [x[0], x[1], snake_block, snake_block])

Message display function

def message(msg, color):
mesg = font_style.render(msg, True, color)
display.blit(mesg, [width / 6, height / 3])

Game loop

def gameLoop():
game_over = False
game_close = False

# Initial position and direction of the snake
x1 = width / 2
y1 = height / 2
x1_change = 0
y1_change = 0

snake_list = []
length_of_snake = 1

# Position of the food
foodx = round(random.randrange(0, width - snake_block) / 20.0) * 20
foody = round(random.randrange(0, height - snake_block) / 20.0) * 20

while not game_over:
    while game_close == True:
        display.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:
                    gameLoop()

    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
    display.fill(white)
    pygame.draw.rect(display, red, [foodx, foody, snake_block, snake_block])
    snake_head = []
    snake_head.append(x1)
    snake_head.append(y1)
    snake_list.append(snake_head)
    if len(snake_list) > length_of_snake:
        del snake_list[0]

    for x in snake_list[:-1]:
        if x == snake_head:
            game_close = True

    our_snake(snake_block, snake_list)

    pygame.display.update()

    if x1 == foodx and y1 == foody:
        foodx = round(random.randrange(0, width - snake_block) / 20.0) * 20
        foody = round(random.randrange(0, height - snake_block) / 20.0) * 20
        length_of_snake += 1

    pygame.time.Clock().tick(snake_speed)

pygame.quit()
quit()

gameLoop()
<>

@Crafter0987
Copy link
Author

Nothing

@joereynolds
Copy link

Format the code correctly using backticks to denote a code block

Like this

And describe the nature of the problem and you might get more responses :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants