Skip to content

Refreshed version of basic game #1

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions MARWIN_ADVENTURE.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
from button import Button
import pygame
import sys
import settings as config
pygame.init()

# Screen state flags
is_darkening = False # Start darkening animation
is_finished_darkening = False # Darkening animation finished
overlay_alpha = 0 # Darkness value

overlay_surface = pygame.Surface((config.XWIN, config.YWIN)) # Create surface for dimming
window = pygame.display.set_mode(config.DISPLAY, config.FLAGS) # Screen
pygame.display.set_caption("MARWIN ADVENTURE") # Set title
pygame.display.set_icon(pygame.image.load(config.icon)) # Set icon

BG = pygame.image.load(config.main_menu_background) # Background

# Button images
play_button_image = pygame.image.load(config.start_button_non_active) # START
exit_button_image = pygame.image.load(config.exit_button_non_active) # EXIT

# Sound flags
play_button_PLAY_sound = False # Play button sound activation
play_button_EXIT_sound = False # Exit button sound activation

# Darkening animation
def darken_screen():
global overlay_alpha, is_darkening, is_finished_darkening # Global variables
overlay_alpha += 5 # Increase darkness value
if overlay_alpha >= 255:
# Animation completion
is_darkening = False
is_finished_darkening = True
overlay_surface.set_alpha(overlay_alpha) # Set darkness
if config.music_volume > 0.0:
config.music_volume -= 0.001
pygame.mixer.music.set_volume(config.music_volume) # Apply reduced volume
window.blit(overlay_surface, (0, 0)) # Draw

# Button action functions
def start_game(): # Start game for START button
global is_darkening
is_darkening = True
play_button.clicked = True
darken_screen()

def exit_game(): # Exit game for EXIT button
pygame.quit()
sys.exit()

# Text display
def display_text(text, font_size, color, font_path, pos):
font = pygame.font.Font(font_path, font_size)
text_render = font.render(text, True, color)
text_rect = text_render.get_rect(center=pos)
window.blit(text_render, text_rect)

# Button instances
play_button = Button(play_button_image, (config.XWIN // 2, config.YWIN // 2), action=start_game)
exit_button = Button(exit_button_image, (config.XWIN // 2, config.YWIN // 1.35), action=exit_game)

def Run():
global overlay_alpha, is_darkening, is_finished_darkening, play_button_PLAY_sound, play_button_EXIT_sound
# Reset before loop start
config.music_volume = 0.2
is_finished_darkening = False
is_darkening = False
overlay_alpha = 0

# Load animation background
bg_y = 0 # Background coordinates
bg_speed = 1 # Background scroll speed

# Start music
pygame.mixer.music.load(config.overworld_music)
pygame.mixer.music.set_volume(config.music_volume)
pygame.mixer.music.play(-1)

while True:
mouse_pos = pygame.mouse.get_pos() # Handle mouse position
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# Button function activation on click
play_button.check_click(mouse_pos)
exit_button.check_click(mouse_pos)

# If darkening animation finished, launch main.py
if is_finished_darkening:
pygame.mixer.music.stop()
import main # Import
main.run_game() # Initialization
# Reset parameters after launch
is_finished_darkening = False
is_darkening = False
overlay_alpha = 0

# Display background and buttons
window.blit(BG, (0, bg_y))
window.blit(BG, (0, bg_y - config.YWIN)) # Extra display for covering image boundary

# Update background position for vertical scrolling
bg_y += bg_speed
if bg_y >= config.YWIN:
bg_y = 0

# Display buttons and handle their hover reactions
if not is_darkening or not is_finished_darkening: # If not in darkening animation
# Play button
if play_button.rect.collidepoint(mouse_pos):
if play_button_PLAY_sound:
config.check_button.play()
play_button_PLAY_sound = False
window.blit(pygame.image.load(config.start_button_active), play_button.rect)
else:
play_button_PLAY_sound = True
window.blit(play_button.image, play_button.rect)

# Exit button
if exit_button.rect.collidepoint(mouse_pos):
if play_button_EXIT_sound:
config.check_button.play()
play_button_EXIT_sound = False
window.blit(pygame.image.load(config.exit_button_active), exit_button.rect)
else:
play_button_EXIT_sound = True
window.blit(exit_button.image, exit_button.rect)

# Display MARWIN image
display_text("MARWIN", 80, config.DARK_BLUE, config.pixel_font, (config.XWIN // 2, config.YWIN // 6))
display_text("ADVENTURE", 80, config.DARK_BLUE, config.pixel_font, (config.XWIN // 2, config.YWIN // 3.5))

if is_darkening: # Screen darkening at game start
darken_screen()

pygame.display.update() # Update screen
pygame.time.Clock().tick(config.FPS) # Set FPS

def main(): # Function to initialize launch, allowing to return to the main menu while in the game
Run()

if __name__ == "__main__":
# Program start
main()
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Pygame DoodleJump
The simplest doodle jump made with python3 and pygame in only 2 days
# Pygame DoodleJump (MARWIN ADVENTURE)
### The game like a doodle jump game based on original code with using a pygame

## Table of contents
* [General info](#general-info)
* [Requirements](#requirements)
* [Setup](#setup)

![Screenshot](https://github.com/MykleCode/pygame-doodlejump/blob/main/demo.gif)

<img src="https://github.com/kostrv/Pygame-DoodleJump-ULTRA-UPGRADED/raw/main/screenshots/preview_2.jpg" alt="Screenshot" width="400" height="400"> <img src="https://github.com/kostrv/Pygame-DoodleJump-ULTRA-UPGRADED/raw/main/screenshots/preview_3.jpg" alt="Screenshot" width="400" height="400"> <img src="https://github.com/kostrv/Pygame-DoodleJump-ULTRA-UPGRADED/raw/main/screenshots/preview_4.jpg" alt="Screenshot" width="400" height="400"> <img src="https://github.com/kostrv/Pygame-DoodleJump-ULTRA-UPGRADED/raw/main/screenshots/preview_1.jpg" alt="Screenshot" width="400" height="400">


## General Info
* No images used for graphics
* Work has been done to improve the visual
* Well clean and organised code
* Relatively small code
* Code is optimized and performant
* Every part of the code is uncommented

## Requirements
* [Python3](https://www.python.org/downloads/)
Expand All @@ -21,3 +21,4 @@ The simplest doodle jump made with python3 and pygame in only 2 days
* Download zip, fork or clone.
* Install requirements
* And Run !
* If you have any problems contact me !
Binary file added audio/effects/activate_button.ogg
Binary file not shown.
Binary file added audio/effects/check_button.ogg
Binary file not shown.
Binary file added audio/effects/explosion.ogg
Binary file not shown.
Binary file added audio/effects/game_over.ogg
Binary file not shown.
Binary file added audio/effects/jump.ogg
Binary file not shown.
Binary file added audio/effects/stomp.ogg
Binary file not shown.
Binary file added audio/level_music.wav
Binary file not shown.
Binary file added audio/overworld_music.wav
Binary file not shown.
14 changes: 14 additions & 0 deletions button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pygame

# Class for management buttons in interface
class Button(pygame.sprite.Sprite):
def __init__(self, image, pos, action=None):
super().__init__()
self.image = image
self.rect = self.image.get_rect(center=pos)
self.action = action # Triggered function

# Run function on mouse click
def check_click(self, mouse_pos):
if self.rect.collidepoint(mouse_pos): # If there is collision
self.action() # Launching
102 changes: 33 additions & 69 deletions camera.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,37 @@
# -*- coding: utf-8 -*-
"""
CopyLeft 2021 Michael Rouves

This file is part of Pygame-DoodleJump.
Pygame-DoodleJump is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Pygame-DoodleJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with Pygame-DoodleJump. If not, see <https://www.gnu.org/licenses/>.
"""


from pygame import Rect
from pygame.sprite import Sprite

import pygame
from singleton import Singleton
import settings as config



class Camera(Singleton):
"""
A class to represent the camera.

Manages level position scrolling.
Can be access via Singleton: Camera.instance.
(Check Singleton design pattern for more info)
"""
# constructor called on new instance: Camera()
def __init__(self, lerp=5,width=config.XWIN, height=config.YWIN):
self.state = Rect(0, 0, width, height)
self.lerp = lerp
self.center = height//2
self.maxheight = self.center

def reset(self) -> None:
" Called only when game restarts (after player death)."
self.state.y = 0
self.maxheight = self.center

def apply_rect(self,rect:Rect) -> Rect:
""" Transforms given rect relative to camera position.
:param rect pygame.Rect: the rect to transform
"""
return rect.move((0,-self.state.topleft[1]))

def apply(self, target:Sprite) -> Rect:
""" Returns new target render position based on current camera position.
:param target Sprite: a sprite that wants to get its render position.
"""
return self.apply_rect(target.rect)

def update(self, target:Rect) -> None:
""" Scrolls up to maxheight reached by player.
Should be called each frame.
:param target pygame.Rect: the target position to follow.
"""
# updating maxheight
if(target.y<self.maxheight):
self.lastheight = self.maxheight
self.maxheight = target.y
# calculate scrolling speed required
speed = ((self.state.y+self.center)-self.maxheight)/self.lerp
self.state.y-=speed

def __init__(self, lerp=8, width=config.XWIN, height=config.YWIN):
# Initialize camera parameters: lerp (camera follow delay based on player's coordinates), width, height
self.state = pygame.Rect(0, 0, width, height) # Camera state
self.lerp = lerp
self.center = height // 2 # Camera center
self.maxheight = self.center # Maximum height

def reset(self):
# Reset function
self.state.y = 0
self.maxheight = self.center

# Transform a rect relative to the camera's position
def apply_rect(self, rect: pygame.Rect) -> pygame.Rect:
# Take a rect as input and return a new rect moved by the negative y-coordinate of the top-left point
return rect.move((0, -self.state.topleft[1]))

# Return a new target rendering position based on the current camera position
def apply(self, target: pygame.sprite.Sprite) -> pygame.Rect:
# Take a rect as input and a Sprite (a sprite that wants to get a rendering position)
# Return the required rect obtained from the apply_rect function, representing the rendering position for the target sprite
return self.apply_rect(target.rect)

# Method to move up to the maximum height achieved by the player
def update(self, target: pygame.Rect):
# Update the maximum height
if target.y < self.maxheight: # If the player reaches a new height on the screen
self.lastheight = self.maxheight
self.maxheight = target.y
# Calculate the required camera follow speed
speed = ((self.state.y + self.center) - self.maxheight) / self.lerp
self.state.y -= speed
Binary file removed demo.gif
Binary file not shown.
Binary file added graphics/UI/frame.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 graphics/UI/m.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 graphics/background/background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 graphics/bonus/explosion/1.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 graphics/bonus/explosion/2.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 graphics/bonus/explosion/3.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 graphics/bonus/explosion/4.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 graphics/bonus/explosion/5.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 graphics/bonus/explosion/6.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 graphics/character/fall/2.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 graphics/character/fall/3.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 graphics/character/fall/4.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 graphics/character/fall/5.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 graphics/character/jump/1.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 graphics/character/jump/2.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 graphics/font/1.psd
Binary file not shown.
Binary file added graphics/font/1проект.psd
Binary file not shown.
Binary file added graphics/font/font.ttf
Binary file not shown.
Binary file added graphics/font/выфвфвфыв.psd
Binary file not shown.
Binary file added graphics/game_over_menu/buttons/active/main_menu.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 graphics/game_over_menu/buttons/active/restart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 graphics/game_over_menu/game_over.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 graphics/main_menu/background/background.png
Binary file added graphics/main_menu/button/active/exit_btn.png
Binary file added graphics/main_menu/button/active/start_btn.png
Binary file added graphics/main_menu/button/non_active/exit_btn.png
Binary file added graphics/main_menu/button/non_active/start_btn.png
Binary file added graphics/main_menu/marwin.png
Binary file added graphics/pause/buttons/active/resume.png
Binary file added graphics/pause/buttons/non_active/resume.png
Binary file added graphics/pause/frame.png
Binary file added graphics/platform/broken/broken-platform.png
Binary file added graphics/platform/normal/platform.png
Loading