Giter Site home page Giter Site logo

snake-game's Introduction

import pygame import random

Game Variables

snake_speed = 15 window_x = 720 window_y = 480 black = pygame.Color(0, 0, 0) white = pygame.Color(255, 255, 255) green = pygame.Color(0, 255, 0)

Initialising pygame

pygame.init() pygame.display.set_caption('Snake Game') game_window = pygame.display.set_mode((window_x, window_y)) fps = pygame.time.Clock()

Initialising snake position and size

snake_position = [100, 50] snake_body = [[100, 50], [90, 50], [80, 50], [70, 50]] fruit_position = [random.randrange(1, (window_x//10)) * 10, random.randrange(1, (window_y//10)) * 10] fruit_spawn = True direction = 'RIGHT'

Game loop

while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() # Whenever a key is pressed down elif event.type == pygame.KEYDOWN: # W -> Up; S -> Down; A -> Left; D -> Right if event.key == pygame.K_UP or event.key == ord('w'): direction = 'UP' if event.key == pygame.K_DOWN or event.key == ord('s'): direction = 'DOWN' if event.key == pygame.K_LEFT or event.key == ord('a'): direction = 'LEFT' if event.key == pygame.K_RIGHT or event.key == ord('d'): direction = 'RIGHT'

# Movement of 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
snake_body.insert(0, list(snake_position))
if snake_position[0] == fruit_position[0] and snake_position[1] == fruit_position[1]:
    fruit_spawn = False
else:
    snake_body.pop()

# Fruit generation
if not fruit_spawn:
    fruit_position = [random.randrange(1, (window_x//10)) * 10, random.randrange(1, (window_y//10)) * 10]
fruit_spawn = True

# GFX
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 Loop
if snake_position[0] < 0 or snake_position[0] > window_x-10:
    game_over()
if snake_position[1] < 0 or snake_position[1] > window_y-10:
    game_over()

# Touching the snake body
for block in snake_body[1:]:
    if snake_position[0] == block[0] and snake_position[1] == block[1]:
        game_over()

# Refresh game screen
pygame.display.update()
# Frame Per Second /Refresh Rate
fps.tick(snake_speed)

snake-game's People

Contributors

cyberjashan01 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.