Skip to content

Commit

Permalink
Add assets for food and snake, and also snake can eat food.
Browse files Browse the repository at this point in the history
  • Loading branch information
xHariz committed Apr 18, 2024
1 parent 3ef89ea commit b758e54
Show file tree
Hide file tree
Showing 18 changed files with 29 additions and 9 deletions.
Binary file modified __pycache__/constants.cpython-312.pyc
Binary file not shown.
Binary file added assets/apple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/body_bottomleft.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/body_bottomright.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/body_horizontal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/body_topleft.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/body_topright.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/body_vertical.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/head_down.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/head_left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/head_right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/head_up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/tail_down.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/tail_left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/tail_right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/tail_up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 28 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import pygame, sys
import pygame, sys, os
from pygame.locals import *
import constants
import random

pygame.init()

# Global scope variables
screen = pygame.display.set_mode((constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT), RESIZABLE)
screen = pygame.display.set_mode((constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT))
icon = pygame.image.load('redblazer.png')
pygame.display.set_icon(icon)
pygame.display.set_caption('Hebi Game')
clock = pygame.time.Clock()

#assets from opengameart.org
APPLE = pygame.image.load(os.path.join('assets', 'apple.png'))
HEAD_UP = pygame.image.load(os.path.join('assets', 'head_up.png'))
HEAD_DOWN = pygame.image.load(os.path.join('assets', 'head_down.png'))
HEAD_RIGHT = pygame.image.load(os.path.join('assets', 'head_right.png'))
HEAD_LEFT = pygame.image.load(os.path.join('assets', 'head_left.png'))


player = pygame.Rect((constants.SCREEN_WIDTH / 2, constants.SCREEN_HEIGHT / 2, 20, 20))
food = pygame.Rect((random.randint(0, 40) * constants.TILESIZE, random.randint(0, 30) * constants.TILESIZE, 20, 20))

class Hebi:
def __init__(self):
Expand All @@ -23,26 +30,39 @@ def __init__(self):
def main():
IN_GAME = True

food = pygame.Rect((random.randint(0, 40) * constants.TILESIZE, random.randint(0, 30) * constants.TILESIZE, 20, 20))
head_direction = HEAD_DOWN

while IN_GAME:
for event in pygame.event.get():
if event.type == pygame.QUIT:
IN_GAME = False

x, y = screen.get_size()
offsetX, offsetY = screen.get_size()
screen.fill((constants.BLACK))
pygame.draw.rect(screen, (255, 0, 0), player)
pygame.draw.rect(screen, (200, 0, 100), food)
screen.blit(head_direction, (player.x, player.y))
screen.blit(APPLE, (food.x, food.y))

key = pygame.key.get_pressed()
if key[pygame.K_a] == True and player.left > 0:
head_direction = HEAD_LEFT
player.move_ip(-constants.TILESIZE, 0)
elif key[pygame.K_d] == True and player.right < constants.SCREEN_WIDTH:
elif key[pygame.K_d] == True and player.right < offsetX:
head_direction = HEAD_RIGHT
player.move_ip(constants.TILESIZE, 0)
elif key[pygame.K_w] == True and player.top > 0:
head_direction = HEAD_UP
player.move_ip(0, -constants.TILESIZE)
elif key[pygame.K_s] == True and player.bottom < constants.SCREEN_HEIGHT:
elif key[pygame.K_s] == True and player.bottom < offsetY:
head_direction = HEAD_DOWN
player.move_ip(0, constants.TILESIZE)

eat = pygame.Rect.colliderect(player, food)

if eat:
food.x = random.randint(0, 40) * constants.TILESIZE
food.y = random.randint(0, 30) * constants.TILESIZE

pygame.display.flip()
clock.tick(16) #Limit 10 FPS for smoother movements

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setuptools.setup(
name='hebi-game',
version='1.0.0',
version='1.0.1',
author='Hariz Asyraf',
description='A 2D Snake Game',
packages=setuptools.find_packages()
Expand Down

0 comments on commit b758e54

Please sign in to comment.