diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index b6e4761..63d55e9 --- a/.gitignore +++ b/.gitignore @@ -1,129 +1,3 @@ -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -pip-wheel-metadata/ -share/python-wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -.python-version - -# pipenv -# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. -# However, in case of collaboration, if having platform-specific dependencies or dependencies -# having no cross-platform support, pipenv may install dependencies that don't work, or not -# install all needed dependencies. -#Pipfile.lock - -# PEP 582; used by e.g. github.com/David-OConnor/pyflow -__pypackages__/ - -# Celery stuff -celerybeat-schedule -celerybeat.pid - -# SageMath parsed files -*.sage.py - -# Environments -.env -.venv -env/ -venv/ -ENV/ -env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ +*.pyc +/**/*.pyc +.idea/ diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 76088c4..dbdf327 --- a/README.md +++ b/README.md @@ -1,2 +1,16 @@ -# proto-pygame -A prototype of a game I did long ago with the library pygame +# PYGAME BASICS DEMO + +This was originally developed to learn the basics of PyGame, such as loading sprites into the screen and collision detection. +It was part of an university project in October 2012. + +### Controlling the car +Use the arrow keys to move the car through the screen. + + +### Updates + +Now I've taken off the old graphics and added some sprites from some old NES video games. I redesigned the screen collisions. Just kept a few rectangles so the car doesn't go outside of the screen. Added rotation of the car's sprite (the shadow of the car doesn't make any sense!). See it working here: https://youtu.be/vG2cFlU25kE + +### Legal stuff + +Graphics taken from some old game's sprites. Music by *Haddaway*. This was made to learn, no copyright violation intended. diff --git a/game.py b/game.py new file mode 100755 index 0000000..c14d776 --- /dev/null +++ b/game.py @@ -0,0 +1,167 @@ +# Juego prueba -Jueves 4 / 10 / 2012- escuela 200, MakeUy +# NEXO 2012, Nicolas Novalic +import pygame +import random +import pygame + +SPRITE_SIZE = 62 +SPRITE_STEP = 20 +SCREEN_SIZE_X = 1240 +SCREEN_SIZE_Y = 744 +INITIAL_POSITION_PLAYER_X = SCREEN_SIZE_X / 2 - SPRITE_SIZE +INITIAL_POSITION_PLAYER_Y = SCREEN_SIZE_Y / 2 + +#GRAPHICS +WATER_IMG = pygame.image.load("media/water.png") +COAST_IMG = pygame.image.load("media/coast.png") +SAND_IMG = pygame.image.load("media/floor.png") + + +class Recs(object): + def __init__(self): + self.collision_list=[] + self.collision_list.append(pygame.Rect(0, 0, 20, SCREEN_SIZE_Y)) # CANT GO OFF SCREEN ON THE LEFT + self.collision_list.append(pygame.Rect(SCREEN_SIZE_X - 20, 0, 20, SCREEN_SIZE_Y)) # DITTO RIGHT + self.collision_list.append(pygame.Rect(0, SCREEN_SIZE_Y - 20, SCREEN_SIZE_X, 20)) # DITTO DOWN + self.collision_list.append(pygame.Rect(0, 0, SCREEN_SIZE_X, 62)) # DITTO UP + +class Player(pygame.sprite.Sprite): + def __init__(self, player_img): + self.player_img = player_img + self.rect = self.player_img.get_rect() + self.rect.top,self.rect.left = (INITIAL_POSITION_PLAYER_Y, INITIAL_POSITION_PLAYER_X) + + def change_position(self,vx,vy): + self.rect.move_ip(vx,vy) + + def update(self,layer): + layer.blit(self.player_img, self.rect) + + def rotate_position(self, angle): + self.player_img = pygame.transform.rotate(self.player_img, angle) + +def collision_detector(player,recs): + for rec in recs.collision_list: + if player.rect.colliderect(rec): + return True + return False + + + +def main(): + + #SCREEN SETUP + pygame.init() + screen = pygame.display.set_mode((SCREEN_SIZE_X, SCREEN_SIZE_Y)) + pygame.display.set_caption("8 bit Game Demo") + game_clock = pygame.time.Clock() + PLAYER_IMG = pygame.image.load("media/car.png").convert_alpha() + #MUSIC + pygame.mixer.music.load("media/music.mp3") + pygame.mixer.music.set_volume(0.5) + pygame.mixer.music.play(-1) + #GAME SET UP + quit_game = False + vx,vy = 0,0 + screen_recs = Recs() + player1 = Player(PLAYER_IMG) + collision = False + direction = "UP" + + #MAIN LOOP + while quit_game != True: + + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + quit_game = True + + #MOVEMENT + if collision == False: + if event.type == pygame.KEYDOWN: + + if event.key == pygame.K_LEFT: + if direction == "UP": + player1.rotate_position(90) + elif direction == "RIGHT": + player1.rotate_position(180) + elif direction == "DOWN": + player1.rotate_position(270) + direction = "LEFT" + + player1.change_position(-1 * SPRITE_STEP, 0) + if collision_detector(player1,screen_recs): + player1.change_position(SPRITE_STEP, 0) + + if event.key == pygame.K_RIGHT: + if direction == "DOWN": + player1.rotate_position(90) + elif direction == "LEFT": + player1.rotate_position(180) + elif direction == "UP": + player1.rotate_position(270) + direction = "RIGHT" + + player1.change_position(SPRITE_STEP, 0) + if collision_detector(player1,screen_recs): + player1.change_position(-1 * SPRITE_STEP, 0) + + if event.key == pygame.K_UP: + if direction == "RIGHT": + player1.rotate_position(90) + elif direction == "DOWN": + player1.rotate_position(180) + elif direction == "LEFT": + player1.rotate_position(270) + direction = "UP" + + player1.change_position(0,-1 * SPRITE_STEP) + if collision_detector(player1, screen_recs): + player1.change_position(0, SPRITE_STEP) + + if event.key == pygame.K_DOWN: + if direction == "LEFT": + player1.rotate_position(90) + elif direction == "UP": + player1.rotate_position(180) + elif direction == "RIGHT": + player1.rotate_position(270) + direction = "DOWN" + + player1.change_position(0, SPRITE_STEP) + if collision_detector(player1, screen_recs): + player1.change_position(0,-1 * SPRITE_STEP) + + + if event.type == pygame.KEYUP: + if event.key == pygame.K_3: + vx = 0 + if event.key == pygame.K_2: + vx = 0 + if event.key== pygame.K_SPACE: + vy = 0 + if event.key == pygame.K_4: + vy = 0 + + game_clock.tick(20) + if collision == False: + player1.change_position(vx, vy) + + + # PRINT GRAPHICS + range_x = SCREEN_SIZE_X / SPRITE_SIZE + range_y = SCREEN_SIZE_Y / SPRITE_SIZE + for printing_x in range(0, range_x): + for printing_y in range(1, range_y): + screen.blit(SAND_IMG, (SPRITE_SIZE * printing_x, SPRITE_SIZE * printing_y)) + + for printing in range(0, range_x): + screen.blit(WATER_IMG, (SPRITE_SIZE * printing,0)) + screen.blit(COAST_IMG, (SPRITE_SIZE * printing, SPRITE_SIZE)) + + player1.update(screen) + pygame.display.update() + + pygame.quit() + +main() diff --git a/media/car.png b/media/car.png new file mode 100755 index 0000000..b10ba1b Binary files /dev/null and b/media/car.png differ diff --git a/media/coast.png b/media/coast.png new file mode 100755 index 0000000..460da6b Binary files /dev/null and b/media/coast.png differ diff --git a/media/floor.png b/media/floor.png new file mode 100755 index 0000000..c3b499a Binary files /dev/null and b/media/floor.png differ diff --git a/media/music.mp3 b/media/music.mp3 new file mode 100644 index 0000000..758ec8c Binary files /dev/null and b/media/music.mp3 differ diff --git a/media/water.png b/media/water.png new file mode 100755 index 0000000..957bdfb Binary files /dev/null and b/media/water.png differ