From c5d04698219c32ccdf80ad8e5e855fc34c77e380 Mon Sep 17 00:00:00 2001 From: Jan Poonthong Date: Thu, 22 Apr 2021 09:08:35 +0700 Subject: [PATCH 1/6] Fix issue #11, name 'colorsys' is not defined Fix: Traceback (most recent call last): File "main.py", line 277, in main() File "main.py", line 161, in main o.setSprite(((player.position.y/50) % 100) / 100) File "/home/jan/Tmp/Flappuccino/background.py", line 9, in setSprite color = colorsys.hsv_to_rgb(tint,1,1) NameError: name 'colorsys' is not defined --- background.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/background.py b/background.py index 097a092..fc7c553 100644 --- a/background.py +++ b/background.py @@ -1,4 +1,4 @@ -import pygame +import pygame, colorsys class Background: def __init__(self): self.sprite = pygame.image.load('data/gfx/bg.png') @@ -8,4 +8,4 @@ def setSprite(self, tint): copy = self.uncoloredSprite.copy() color = colorsys.hsv_to_rgb(tint,1,1) copy.fill((color[0]*255, color[1]*255, color[2]*255), special_flags=pygame.BLEND_ADD) - self.sprite = copy \ No newline at end of file + self.sprite = copy From 1ee88e7916c9e6f3381a383c2f3ce3907efda476 Mon Sep 17 00:00:00 2001 From: Jan Poonthong Date: Thu, 22 Apr 2021 09:09:53 +0700 Subject: [PATCH 2/6] Fix issue attempted relative import with no known parent package Fix: Traceback (most recent call last): File "main.py", line 4, in from .player import Player ImportError: attempted relative import with no known parent package --- main.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index a4256fa..1d5a5b1 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,12 @@ import pygame, sys, time, random, colorsys, math from pygame.math import Vector2 from pygame.locals import * -from .player import Player -from .background import Background -from .button import Button -from .bean import Bean -from .utils import clamp -from .utils import checkCollisions +from player import Player +from background import Background +from button import Button +from bean import Bean +from utils import clamp +from utils import checkCollisions def main(): From 32ddf68405a8bc288d0253e6c420ed38f6dc65b7 Mon Sep 17 00:00:00 2001 From: Jan Poonthong Date: Thu, 22 Apr 2021 09:23:09 +0700 Subject: [PATCH 3/6] Update docs by adding link to Python and Pygame --- README.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 022bf1f..797d415 100644 --- a/README.md +++ b/README.md @@ -2,26 +2,31 @@ Flappuccino is a game created in 48 hours for the [PyGame Community New Years Jam](https://itch.io/jam/pygame-community-jam) using Python with [Pygame](https://www.pygame.org). ## Screenshots + ![](https://img.itch.zone/aW1hZ2UvODg3MDQ0LzUwMDQzOTkuZ2lm/original/vd0wHu.gif) ## Background + Information on how to play is available on the game's [itch.io page](https://polymars.itch.io/flappuccino). ## Usage + ### Releases + A Windows build of the game is available [here](https://polymars.itch.io/flappuccino). + ### Running from source -Grab the latest release of Python from [here](https://www.python.org/downloads/) **and** install Pygame by executing ``pip install pygame``. + +Grab the latest release of [Python](https://www.python.org/downloads/) **and** install [Pygame](https://www.pygame.org/wiki/GettingStarted) by executing ``pip install pygame``. **Note:** If the ``pip install pygame`` did not work for you, then try this: -1. Windows: +1. Windows and Linux: ``python -m pip install pygame`` 2. Mac: ``python3 -m pip install pygame`` -3. Linux: -Same as windows. Ensure ``main.py`` is in the same directory as ``./data`` and execute ``python main.py``. ## Contributing + Pull requests are welcome! For major refactors, please open an issue first to discuss what you would like to improve. Feel free to create a fork of this repository and use the code for any noncommercial purposes. From 86fd6e0f9af0bc08379904456a022458a5db7c93 Mon Sep 17 00:00:00 2001 From: Jan Poonthong Date: Fri, 23 Apr 2021 09:24:45 +0700 Subject: [PATCH 4/6] Enable running the game from anywhere, no need for cd into the game So now you could run your games like python3 /home/PolyMarsDev/games/Flappuccino/main.py --- background.py | 6 +++--- bean.py | 6 +++--- button.py | 6 +++--- main.py | 49 ++++++++++++++++++++++++++----------------------- player.py | 17 +++++++++-------- 5 files changed, 44 insertions(+), 40 deletions(-) diff --git a/background.py b/background.py index fc7c553..09dff73 100644 --- a/background.py +++ b/background.py @@ -1,9 +1,9 @@ import pygame, colorsys class Background: - def __init__(self): - self.sprite = pygame.image.load('data/gfx/bg.png') + def __init__(self, BASE_PATH): + self.sprite = pygame.image.load(BASE_PATH + '/data/gfx/bg.png') self.position = 0 - self.uncoloredSprite = pygame.image.load('data/gfx/bg.png') + self.uncoloredSprite = pygame.image.load(BASE_PATH + '/data/gfx/bg.png') def setSprite(self, tint): copy = self.uncoloredSprite.copy() color = colorsys.hsv_to_rgb(tint,1,1) diff --git a/bean.py b/bean.py index 5b212bb..c0737a8 100644 --- a/bean.py +++ b/bean.py @@ -1,6 +1,6 @@ import pygame class Bean: - def __init__(self): - self.sprite = pygame.image.load('data/gfx/bean.png') + def __init__(self, BASE_PATH): + self.sprite = pygame.image.load(BASE_PATH + '/data/gfx/bean.png') self.position = pygame.Vector2() - self.position.xy \ No newline at end of file + self.position.xy diff --git a/button.py b/button.py index ef92dfb..898b05e 100644 --- a/button.py +++ b/button.py @@ -1,7 +1,7 @@ import pygame class Button: - def __init__(self): + def __init__(self, BASE_PATH): self.price = 3 self.level = 1 - sprite = pygame.image.load('data/gfx/button.png') - typeIndicatorSprite = pygame.image.load('data/gfx/null_indicator.png') + self.sprite = pygame.image.load(BASE_PATH + '/data/gfx/button.png') + self.typeIndicatorSprite = pygame.image.load(BASE_PATH + '/data/gfx/null_indicator.png') diff --git a/main.py b/main.py index 1d5a5b1..708a214 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ import pygame, sys, time, random, colorsys, math +from os.path import abspath, dirname from pygame.math import Vector2 from pygame.locals import * from player import Player @@ -12,50 +13,52 @@ def main(): pygame.init() # set the display + BASE_PATH = abspath(dirname(__file__)) DISPLAY=pygame.display.set_mode((640,480),0,32) pygame.display.set_caption('Flappuccino') - pygame.display.set_icon(Bean().sprite) + pygame.display.set_icon(Bean(BASE_PATH).sprite) # get fonts - font = pygame.font.Font('data/fonts/font.otf', 100) - font_small = pygame.font.Font('data/fonts/font.otf', 32) - font_20 = pygame.font.Font('data/fonts/font.otf', 20) + FONT = '/data/fonts/font.otf' + font = pygame.font.Font(BASE_PATH + FONT, 100) + font_small = pygame.font.Font(BASE_PATH + FONT, 32) + font_20 = pygame.font.Font(BASE_PATH + FONT, 20) # get some images - shop = pygame.image.load('data/gfx/shop.png') - shop_bg = pygame.image.load('data/gfx/shop_bg.png') - retry_button = pygame.image.load('data/gfx/retry_button.png') - logo = pygame.image.load('data/gfx/logo.png') - title_bg = pygame.image.load('data/gfx/bg.png') + shop = pygame.image.load(BASE_PATH + '/data/gfx/shop.png') + shop_bg = pygame.image.load(BASE_PATH + '/data/gfx/shop_bg.png') + retry_button = pygame.image.load(BASE_PATH + '/data/gfx/retry_button.png') + logo = pygame.image.load(BASE_PATH + '/data/gfx/logo.png') + title_bg = pygame.image.load(BASE_PATH + '/data/gfx/bg.png') title_bg.fill((255, 30.599999999999998, 0.0), special_flags=pygame.BLEND_ADD) - shadow = pygame.image.load('data/gfx/shadow.png') + shadow = pygame.image.load(BASE_PATH + '/data/gfx/shadow.png') # get sounds - flapfx = pygame.mixer.Sound("data/sfx/flap.wav") - upgradefx = pygame.mixer.Sound("data/sfx/upgrade.wav") - beanfx = pygame.mixer.Sound("data/sfx/bean.wav") - deadfx = pygame.mixer.Sound("data/sfx/dead.wav") + flapfx = pygame.mixer.Sound(BASE_PATH + "/data/sfx/flap.wav") + upgradefx = pygame.mixer.Sound(BASE_PATH + "/data/sfx/upgrade.wav") + beanfx = pygame.mixer.Sound(BASE_PATH + "/data/sfx/bean.wav") + deadfx = pygame.mixer.Sound(BASE_PATH + "/data/sfx/dead.wav") # colors WHITE=(255,255,255) # constant # variables rotOffset = -5 # creating a new object player - player = Player() + player = Player(BASE_PATH) beans = [] buttons = [] # adding three buttons - for i in range(3): buttons.append(Button()) + for i in range(3): buttons.append(Button(BASE_PATH)) # now simply loading images based off of indexes in the list - buttons[0].typeIndicatorSprite = pygame.image.load('data/gfx/flap_indicator.png') + buttons[0].typeIndicatorSprite = pygame.image.load(BASE_PATH + '/data/gfx/flap_indicator.png') buttons[0].price = 5 - buttons[1].typeIndicatorSprite = pygame.image.load('data/gfx/speed_indicator.png') + buttons[1].typeIndicatorSprite = pygame.image.load(BASE_PATH + '/data/gfx/speed_indicator.png') buttons[1].price = 5 - buttons[2].typeIndicatorSprite = pygame.image.load('data/gfx/beanup_indicator.png') + buttons[2].typeIndicatorSprite = pygame.image.load(BASE_PATH + '/data/gfx/beanup_indicator.png') buttons[2].price = 30 # getting 5 beans - for i in range(5): beans.append(Bean()) + for i in range(5): beans.append(Bean(BASE_PATH)) # now looping through the beans list for bean in beans: bean.position.xy = random.randrange(0, DISPLAY.get_width() - bean.sprite.get_width()), beans.index(bean)*-200 - player.position.y # creating a list of backgrounds, with each index being an object - bg = [Background(), Background(), Background()] + bg = [Background(BASE_PATH), Background(BASE_PATH), Background(BASE_PATH)] # some variables that we need beanCount = 0 startingHeight = player.position.y @@ -251,7 +254,7 @@ def main(): flapForce = 3 beanMultiplier = 5 buttons = [] - for i in range(3): buttons.append(Button()) + for i in range(3): buttons.append(Button(BASE_PATH)) buttons[0].typeIndicatorSprite = pygame.image.load('data/gfx/flap_indicator.png') buttons[0].price = 5 buttons[1].typeIndicatorSprite = pygame.image.load('data/gfx/speed_indicator.png') @@ -259,7 +262,7 @@ def main(): buttons[2].typeIndicatorSprite = pygame.image.load('data/gfx/beanup_indicator.png') buttons[2].price = 30 beans = [] - for i in range(5): beans.append(Bean()) + for i in range(5): beans.append(Bean(BASE_PATH)) for bean in beans: bean.position.xy = random.randrange(0, DISPLAY.get_width() - bean.sprite.get_width()), beans.index(bean)*-200 - player.position.y pygame.mixer.Sound.play(upgradefx) diff --git a/player.py b/player.py index 3f4f819..20344c4 100644 --- a/player.py +++ b/player.py @@ -1,11 +1,12 @@ import pygame class Player: - position = pygame.Vector2() - position.xy = 295, 100 - velocity = pygame.Vector2() - velocity.xy = 3, 0 - acceleration = 0.1 - rightSprite = pygame.image.load('data/gfx/player.png') - leftSprite = pygame.transform.flip(rightSprite, True, False) - currentSprite = rightSprite \ No newline at end of file + def __init__(self, BASE_PATH): + self.position = pygame.Vector2() + self.position.xy = 295, 100 + self.velocity = pygame.Vector2() + self.velocity.xy = 3, 0 + self.acceleration = 0.1 + self.rightSprite = pygame.image.load(BASE_PATH + '/data/gfx/player.png') + self.leftSprite = pygame.transform.flip(self.rightSprite, True, False) + self.currentSprite = self.rightSprite From 4c92e782ce90b7bed495505ffe55192777817f65 Mon Sep 17 00:00:00 2001 From: Jan Poonthong Date: Wed, 14 Sep 2022 00:55:30 +0700 Subject: [PATCH 5/6] Refactor code with black --- README.md | 18 +-- background.py | 16 ++- bean.py | 6 +- button.py | 10 +- main.py | 340 ++++++++++++++++++++++++++++++++++++-------------- player.py | 3 +- utils.py | 8 +- 7 files changed, 290 insertions(+), 111 deletions(-) diff --git a/README.md b/README.md index 797d415..0ed6303 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@ # Flappuccino -Flappuccino is a game created in 48 hours for the [PyGame Community New Years Jam](https://itch.io/jam/pygame-community-jam) using Python with [Pygame](https://www.pygame.org). +Flappuccino is a game created in 48 hours for the [PyGame Community New Years Jam](https://itch.io/jam/pygame-community-jam) using Python with [Pygame](https://www.pygame.org). + ## Screenshots -![](https://img.itch.zone/aW1hZ2UvODg3MDQ0LzUwMDQzOTkuZ2lm/original/vd0wHu.gif) +![](https://img.itch.zone/aW1hZ2UvODg3MDQ0LzUwMDQzOTkuZ2lm/original/vd0wHu.gif) ## Background @@ -17,15 +18,16 @@ A Windows build of the game is available [here](https://polymars.itch.io/flappuc ### Running from source -Grab the latest release of [Python](https://www.python.org/downloads/) **and** install [Pygame](https://www.pygame.org/wiki/GettingStarted) by executing ``pip install pygame``. +Grab the latest release of [Python](https://www.python.org/downloads/) **and** install [Pygame](https://www.pygame.org/wiki/GettingStarted) by executing `pip install pygame`. + +**Note:** If the `pip install pygame` did not work for you, then try this: -**Note:** If the ``pip install pygame`` did not work for you, then try this: 1. Windows and Linux: -``python -m pip install pygame`` -2. Mac: -``python3 -m pip install pygame`` + `python -m pip install pygame` +2. Mac: + `python3 -m pip install pygame` -Ensure ``main.py`` is in the same directory as ``./data`` and execute ``python main.py``. +Ensure `main.py` is in the same directory as `./data` and execute `python main.py`. ## Contributing diff --git a/background.py b/background.py index 09dff73..33b22d0 100644 --- a/background.py +++ b/background.py @@ -1,11 +1,17 @@ import pygame, colorsys + + class Background: def __init__(self, BASE_PATH): - self.sprite = pygame.image.load(BASE_PATH + '/data/gfx/bg.png') + self.sprite = pygame.image.load(BASE_PATH + "/data/gfx/bg.png") self.position = 0 - self.uncoloredSprite = pygame.image.load(BASE_PATH + '/data/gfx/bg.png') - def setSprite(self, tint): + self.uncoloredSprite = pygame.image.load(BASE_PATH + "/data/gfx/bg.png") + + def setSprite(self, tint): copy = self.uncoloredSprite.copy() - color = colorsys.hsv_to_rgb(tint,1,1) - copy.fill((color[0]*255, color[1]*255, color[2]*255), special_flags=pygame.BLEND_ADD) + color = colorsys.hsv_to_rgb(tint, 1, 1) + copy.fill( + (color[0] * 255, color[1] * 255, color[2] * 255), + special_flags=pygame.BLEND_ADD, + ) self.sprite = copy diff --git a/bean.py b/bean.py index c0737a8..db59081 100644 --- a/bean.py +++ b/bean.py @@ -1,6 +1,8 @@ import pygame -class Bean: + + +class Bean: def __init__(self, BASE_PATH): - self.sprite = pygame.image.load(BASE_PATH + '/data/gfx/bean.png') + self.sprite = pygame.image.load(BASE_PATH + "/data/gfx/bean.png") self.position = pygame.Vector2() self.position.xy diff --git a/button.py b/button.py index 898b05e..7c78411 100644 --- a/button.py +++ b/button.py @@ -1,7 +1,11 @@ import pygame + + class Button: def __init__(self, BASE_PATH): self.price = 3 - self.level = 1 - self.sprite = pygame.image.load(BASE_PATH + '/data/gfx/button.png') - self.typeIndicatorSprite = pygame.image.load(BASE_PATH + '/data/gfx/null_indicator.png') + self.level = 1 + self.sprite = pygame.image.load(BASE_PATH + "/data/gfx/button.png") + self.typeIndicatorSprite = pygame.image.load( + BASE_PATH + "/data/gfx/null_indicator.png" + ) diff --git a/main.py b/main.py index 708a214..e1e0537 100644 --- a/main.py +++ b/main.py @@ -14,29 +14,31 @@ def main(): pygame.init() # set the display BASE_PATH = abspath(dirname(__file__)) - DISPLAY=pygame.display.set_mode((640,480),0,32) - pygame.display.set_caption('Flappuccino') + DISPLAY = pygame.display.set_mode((640, 480), 0, 32) + pygame.display.set_caption("Flappuccino") pygame.display.set_icon(Bean(BASE_PATH).sprite) # get fonts - FONT = '/data/fonts/font.otf' + FONT = "/data/fonts/font.otf" font = pygame.font.Font(BASE_PATH + FONT, 100) font_small = pygame.font.Font(BASE_PATH + FONT, 32) font_20 = pygame.font.Font(BASE_PATH + FONT, 20) # get some images - shop = pygame.image.load(BASE_PATH + '/data/gfx/shop.png') - shop_bg = pygame.image.load(BASE_PATH + '/data/gfx/shop_bg.png') - retry_button = pygame.image.load(BASE_PATH + '/data/gfx/retry_button.png') - logo = pygame.image.load(BASE_PATH + '/data/gfx/logo.png') - title_bg = pygame.image.load(BASE_PATH + '/data/gfx/bg.png') - title_bg.fill((255, 30.599999999999998, 0.0), special_flags=pygame.BLEND_ADD) - shadow = pygame.image.load(BASE_PATH + '/data/gfx/shadow.png') + shop = pygame.image.load(BASE_PATH + "/data/gfx/shop.png") + shop_bg = pygame.image.load(BASE_PATH + "/data/gfx/shop_bg.png") + retry_button = pygame.image.load(BASE_PATH + "/data/gfx/retry_button.png") + logo = pygame.image.load(BASE_PATH + "/data/gfx/logo.png") + title_bg = pygame.image.load(BASE_PATH + "/data/gfx/bg.png") + title_bg.fill( + (255, 30.599999999999998, 0.0), special_flags=pygame.BLEND_ADD + ) + shadow = pygame.image.load(BASE_PATH + "/data/gfx/shadow.png") # get sounds flapfx = pygame.mixer.Sound(BASE_PATH + "/data/sfx/flap.wav") upgradefx = pygame.mixer.Sound(BASE_PATH + "/data/sfx/upgrade.wav") beanfx = pygame.mixer.Sound(BASE_PATH + "/data/sfx/bean.wav") deadfx = pygame.mixer.Sound(BASE_PATH + "/data/sfx/dead.wav") # colors - WHITE=(255,255,255) # constant + WHITE = (255, 255, 255) # constant # variables rotOffset = -5 # creating a new object player @@ -44,19 +46,30 @@ def main(): beans = [] buttons = [] # adding three buttons - for i in range(3): buttons.append(Button(BASE_PATH)) + for i in range(3): + buttons.append(Button(BASE_PATH)) # now simply loading images based off of indexes in the list - buttons[0].typeIndicatorSprite = pygame.image.load(BASE_PATH + '/data/gfx/flap_indicator.png') - buttons[0].price = 5 - buttons[1].typeIndicatorSprite = pygame.image.load(BASE_PATH + '/data/gfx/speed_indicator.png') - buttons[1].price = 5 - buttons[2].typeIndicatorSprite = pygame.image.load(BASE_PATH + '/data/gfx/beanup_indicator.png') + buttons[0].typeIndicatorSprite = pygame.image.load( + BASE_PATH + "/data/gfx/flap_indicator.png" + ) + buttons[0].price = 5 + buttons[1].typeIndicatorSprite = pygame.image.load( + BASE_PATH + "/data/gfx/speed_indicator.png" + ) + buttons[1].price = 5 + buttons[2].typeIndicatorSprite = pygame.image.load( + BASE_PATH + "/data/gfx/beanup_indicator.png" + ) buttons[2].price = 30 # getting 5 beans - for i in range(5): beans.append(Bean(BASE_PATH)) + for i in range(5): + beans.append(Bean(BASE_PATH)) # now looping through the beans list for bean in beans: - bean.position.xy = random.randrange(0, DISPLAY.get_width() - bean.sprite.get_width()), beans.index(bean)*-200 - player.position.y + bean.position.xy = ( + random.randrange(0, DISPLAY.get_width() - bean.sprite.get_width()), + beans.index(bean) * -200 - player.position.y, + ) # creating a list of backgrounds, with each index being an object bg = [Background(BASE_PATH), Background(BASE_PATH), Background(BASE_PATH)] # some variables that we need @@ -71,7 +84,7 @@ def main(): framerate = 60 last_time = time.time() splashScreenTimer = 0 - #splash screen + # splash screen # playing a sound pygame.mixer.Sound.play(flapfx) while splashScreenTimer < 100: @@ -83,20 +96,26 @@ def main(): for event in pygame.event.get(): # if the user clicks the button - if event.type==QUIT: + if event.type == QUIT: pygame.quit() sys.exit() DISPLAY.fill((231, 205, 183)) # fill the start message on the top of the game startMessage = font_small.render("POLYMARS", True, (171, 145, 123)) - DISPLAY.blit(startMessage, (DISPLAY.get_width()/2 - startMessage.get_width()/2, DISPLAY.get_height()/2 - startMessage.get_height()/2)) - + DISPLAY.blit( + startMessage, + ( + DISPLAY.get_width() / 2 - startMessage.get_width() / 2, + DISPLAY.get_height() / 2 - startMessage.get_height() / 2, + ), + ) + # update display pygame.display.update() # wait for 10 seconds pygame.time.delay(10) - + titleScreen = True # title screen pygame.mixer.Sound.play(flapfx) @@ -105,7 +124,7 @@ def main(): dt *= 60 last_time = time.time() # get the position of the mouse - mouseX,mouseY = pygame.mouse.get_pos() + mouseX, mouseY = pygame.mouse.get_pos() # getting the keys pressed clicked = False keys = pygame.key.get_pressed() @@ -114,22 +133,46 @@ def main(): if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: clicked = True # if the player quits - if event.type==QUIT: + if event.type == QUIT: pygame.quit() sys.exit() # so the user clicked, and by any change the mouse's position was on the buttons - if (clicked and checkCollisions(mouseX, mouseY, 3, 3, DISPLAY.get_width()/2 - retry_button.get_width()/2, 288, retry_button.get_width(), retry_button.get_height())): + if clicked and checkCollisions( + mouseX, + mouseY, + 3, + 3, + DISPLAY.get_width() / 2 - retry_button.get_width() / 2, + 288, + retry_button.get_width(), + retry_button.get_height(), + ): clicked = False pygame.mixer.Sound.play(upgradefx) titleScreen = False DISPLAY.fill(WHITE) - DISPLAY.blit(title_bg, (0,0)) - DISPLAY.blit(shadow, (0,0)) - DISPLAY.blit(logo, (DISPLAY.get_width()/2 - logo.get_width()/2, DISPLAY.get_height()/2 - logo.get_height()/2 + math.sin(time.time()*5)*5 - 25)) - DISPLAY.blit(retry_button, (DISPLAY.get_width()/2 - retry_button.get_width()/2, 288)) + DISPLAY.blit(title_bg, (0, 0)) + DISPLAY.blit(shadow, (0, 0)) + DISPLAY.blit( + logo, + ( + DISPLAY.get_width() / 2 - logo.get_width() / 2, + DISPLAY.get_height() / 2 + - logo.get_height() / 2 + + math.sin(time.time() * 5) * 5 + - 25, + ), + ) + DISPLAY.blit( + retry_button, + (DISPLAY.get_width() / 2 - retry_button.get_width() / 2, 288), + ) startMessage = font_small.render("START", True, (0, 0, 0)) - DISPLAY.blit(startMessage, (DISPLAY.get_width()/2 - startMessage.get_width()/2, 292)) + DISPLAY.blit( + startMessage, + (DISPLAY.get_width() / 2 - startMessage.get_width() / 2, 292), + ) pygame.display.update() pygame.time.delay(10) @@ -140,59 +183,106 @@ def main(): dt *= 60 last_time = time.time() # again, get the position - mouseX,mouseY = pygame.mouse.get_pos() + mouseX, mouseY = pygame.mouse.get_pos() jump = False clicked = False keys = pygame.key.get_pressed() # get events for event in pygame.event.get(): - if event.type==pygame.KEYDOWN and event.key==K_SPACE: + if event.type == pygame.KEYDOWN and event.key == K_SPACE: jump = True if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: clicked = True if clicked and mouseY < DISPLAY.get_height() - 90: jump = True - if event.type==QUIT: + if event.type == QUIT: pygame.quit() sys.exit() - - camOffset = -player.position.y + DISPLAY.get_height()/2 - player.currentSprite.get_size()[1]/2 - + + camOffset = ( + -player.position.y + + DISPLAY.get_height() / 2 + - player.currentSprite.get_size()[1] / 2 + ) + DISPLAY.fill(WHITE) for o in bg: - o.setSprite(((player.position.y/50) % 100) / 100) + o.setSprite(((player.position.y / 50) % 100) / 100) DISPLAY.blit(o.sprite, (0, o.position)) - color = colorsys.hsv_to_rgb(((player.position.y/50) % 100) / 100,0.5,0.5) - currentHeightMarker = font.render(str(height), True, (color[0]*255, color[1]*255, color[2]*255, 50 )) - DISPLAY.blit(currentHeightMarker, (DISPLAY.get_width()/2 - currentHeightMarker.get_width()/2, camOffset + round((player.position.y - startingHeight)/DISPLAY.get_height())*DISPLAY.get_height() + player.currentSprite.get_height() - 40)) - + color = colorsys.hsv_to_rgb( + ((player.position.y / 50) % 100) / 100, 0.5, 0.5 + ) + currentHeightMarker = font.render( + str(height), + True, + (color[0] * 255, color[1] * 255, color[2] * 255, 50), + ) + DISPLAY.blit( + currentHeightMarker, + ( + DISPLAY.get_width() / 2 - currentHeightMarker.get_width() / 2, + camOffset + + round( + (player.position.y - startingHeight) / DISPLAY.get_height() + ) + * DISPLAY.get_height() + + player.currentSprite.get_height() + - 40, + ), + ) + for bean in beans: - DISPLAY.blit(bean.sprite, (bean.position.x, bean.position.y + camOffset)) - - DISPLAY.blit(pygame.transform.rotate(player.currentSprite, clamp(player.velocity.y, -10, 5)*rotOffset), (player.position.x,player.position.y + camOffset)) + DISPLAY.blit( + bean.sprite, (bean.position.x, bean.position.y + camOffset) + ) + + DISPLAY.blit( + pygame.transform.rotate( + player.currentSprite, + clamp(player.velocity.y, -10, 5) * rotOffset, + ), + (player.position.x, player.position.y + camOffset), + ) DISPLAY.blit(shop_bg, (0, 0)) - pygame.draw.rect(DISPLAY,(81,48,20),(21,437,150*(health/100),25)) + pygame.draw.rect( + DISPLAY, (81, 48, 20), (21, 437, 150 * (health / 100), 25) + ) DISPLAY.blit(shop, (0, 0)) - + for button in buttons: - DISPLAY.blit(button.sprite, (220 + (buttons.index(button)*125), 393)) - priceDisplay = font_small.render(str(button.price), True, (0,0,0)) - DISPLAY.blit(priceDisplay, (262 + (buttons.index(button)*125), 408)) - levelDisplay = font_20.render('Lvl. ' + str(button.level), True, (200,200,200)) - DISPLAY.blit(levelDisplay, (234 + (buttons.index(button)*125), 441)) - DISPLAY.blit(button.typeIndicatorSprite, (202 + (buttons.index(button)*125), 377)) - beanCountDisplay = font_small.render(str(beanCount).zfill(7), True, (0,0,0)) + DISPLAY.blit( + button.sprite, (220 + (buttons.index(button) * 125), 393) + ) + priceDisplay = font_small.render(str(button.price), True, (0, 0, 0)) + DISPLAY.blit( + priceDisplay, (262 + (buttons.index(button) * 125), 408) + ) + levelDisplay = font_20.render( + "Lvl. " + str(button.level), True, (200, 200, 200) + ) + DISPLAY.blit( + levelDisplay, (234 + (buttons.index(button) * 125), 441) + ) + DISPLAY.blit( + button.typeIndicatorSprite, + (202 + (buttons.index(button) * 125), 377), + ) + beanCountDisplay = font_small.render( + str(beanCount).zfill(7), True, (0, 0, 0) + ) DISPLAY.blit(beanCountDisplay, (72, 394)) if dead: DISPLAY.blit(retry_button, (4, 4)) deathMessage = font_small.render("RETRY", True, (0, 0, 0)) DISPLAY.blit(deathMessage, (24, 8)) - - height = round(-(player.position.y - startingHeight)/DISPLAY.get_height()) - - player.position.x += player.velocity.x*dt + + height = round( + -(player.position.y - startingHeight) / DISPLAY.get_height() + ) + + player.position.x += player.velocity.x * dt if player.position.x + player.currentSprite.get_size()[0] > 640: player.velocity.x = -abs(player.velocity.x) player.currentSprite = player.leftSprite @@ -204,47 +294,98 @@ def main(): if jump and not dead: player.velocity.y = -flapForce pygame.mixer.Sound.play(flapfx) - player.position.y += player.velocity.y*dt - player.velocity.y = clamp(player.velocity.y + player.acceleration*dt, -99999999999, 50) + player.position.y += player.velocity.y * dt + player.velocity.y = clamp( + player.velocity.y + player.acceleration * dt, -99999999999, 50 + ) - health -= 0.2*dt + health -= 0.2 * dt if health <= 0 and not dead: dead = True pygame.mixer.Sound.play(deadfx) - for bean in beans: if bean.position.y + camOffset + 90 > DISPLAY.get_height(): - bean.position.y -= DISPLAY.get_height()*2 - bean.position.x = random.randrange(0, DISPLAY.get_width() - bean.sprite.get_width()) - if (checkCollisions(player.position.x, player.position.y, player.currentSprite.get_width(), player.currentSprite.get_height(), bean.position.x, bean.position.y, bean.sprite.get_width(), bean.sprite.get_height())): + bean.position.y -= DISPLAY.get_height() * 2 + bean.position.x = random.randrange( + 0, DISPLAY.get_width() - bean.sprite.get_width() + ) + if checkCollisions( + player.position.x, + player.position.y, + player.currentSprite.get_width(), + player.currentSprite.get_height(), + bean.position.x, + bean.position.y, + bean.sprite.get_width(), + bean.sprite.get_height(), + ): dead = False pygame.mixer.Sound.play(beanfx) beanCount += 1 health = 100 - bean.position.y -= DISPLAY.get_height() - random.randrange(0, 200) - bean.position.x = random.randrange(0, DISPLAY.get_width() - bean.sprite.get_width()) + bean.position.y -= DISPLAY.get_height() - random.randrange( + 0, 200 + ) + bean.position.x = random.randrange( + 0, DISPLAY.get_width() - bean.sprite.get_width() + ) for button in buttons: - buttonX,buttonY = 220 + (buttons.index(button)*125), 393 - if clicked and not dead and checkCollisions(mouseX, mouseY, 3, 3, buttonX, buttonY, button.sprite.get_width(), button.sprite.get_height()): - if (beanCount >= button.price): + buttonX, buttonY = 220 + (buttons.index(button) * 125), 393 + if ( + clicked + and not dead + and checkCollisions( + mouseX, + mouseY, + 3, + 3, + buttonX, + buttonY, + button.sprite.get_width(), + button.sprite.get_height(), + ) + ): + if beanCount >= button.price: pygame.mixer.Sound.play(upgradefx) button.level += 1 beanCount -= button.price - button.price = round(button.price*2.5) - if (buttons.index(button) == 0): + button.price = round(button.price * 2.5) + if buttons.index(button) == 0: flapForce *= 1.5 - if (buttons.index(button) == 1): + if buttons.index(button) == 1: player.velocity.x *= 1.5 - if (buttons.index(button) == 2): + if buttons.index(button) == 2: oldBeanMultipler = beanMultiplier beanMultiplier += 10 for i in range(beanMultiplier): beans.append(Bean()) - beans[-1].position.xy = random.randrange(0, DISPLAY.get_width() - bean.sprite.get_width()), player.position.y - DISPLAY.get_height() - random.randrange(0, 200) - - if dead and clicked and checkCollisions(mouseX, mouseY, 3, 3, 4, 4, retry_button.get_width(), retry_button.get_height()): + beans[-1].position.xy = ( + random.randrange( + 0, + DISPLAY.get_width() + - bean.sprite.get_width(), + ), + player.position.y + - DISPLAY.get_height() + - random.randrange(0, 200), + ) + + if ( + dead + and clicked + and checkCollisions( + mouseX, + mouseY, + 3, + 3, + 4, + 4, + retry_button.get_width(), + retry_button.get_height(), + ) + ): health = 100 player.velocity.xy = 3, 0 player.position.xy = 295, 100 @@ -254,27 +395,44 @@ def main(): flapForce = 3 beanMultiplier = 5 buttons = [] - for i in range(3): buttons.append(Button(BASE_PATH)) - buttons[0].typeIndicatorSprite = pygame.image.load('data/gfx/flap_indicator.png') - buttons[0].price = 5 - buttons[1].typeIndicatorSprite = pygame.image.load('data/gfx/speed_indicator.png') - buttons[1].price = 5 - buttons[2].typeIndicatorSprite = pygame.image.load('data/gfx/beanup_indicator.png') + for i in range(3): + buttons.append(Button(BASE_PATH)) + buttons[0].typeIndicatorSprite = pygame.image.load( + "data/gfx/flap_indicator.png" + ) + buttons[0].price = 5 + buttons[1].typeIndicatorSprite = pygame.image.load( + "data/gfx/speed_indicator.png" + ) + buttons[1].price = 5 + buttons[2].typeIndicatorSprite = pygame.image.load( + "data/gfx/beanup_indicator.png" + ) buttons[2].price = 30 beans = [] - for i in range(5): beans.append(Bean(BASE_PATH)) + for i in range(5): + beans.append(Bean(BASE_PATH)) for bean in beans: - bean.position.xy = random.randrange(0, DISPLAY.get_width() - bean.sprite.get_width()), beans.index(bean)*-200 - player.position.y + bean.position.xy = ( + random.randrange( + 0, DISPLAY.get_width() - bean.sprite.get_width() + ), + beans.index(bean) * -200 - player.position.y, + ) pygame.mixer.Sound.play(upgradefx) - dead = False + dead = False - - bg[0].position = camOffset + round(player.position.y/DISPLAY.get_height())*DISPLAY.get_height() - bg[1].position = bg[0].position + DISPLAY.get_height() + bg[0].position = ( + camOffset + + round(player.position.y / DISPLAY.get_height()) + * DISPLAY.get_height() + ) + bg[1].position = bg[0].position + DISPLAY.get_height() bg[2].position = bg[0].position - DISPLAY.get_height() - + pygame.display.update() pygame.time.delay(10) + if __name__ == "__main__": main() diff --git a/player.py b/player.py index 20344c4..1a04968 100644 --- a/player.py +++ b/player.py @@ -1,5 +1,6 @@ import pygame + class Player: def __init__(self, BASE_PATH): self.position = pygame.Vector2() @@ -7,6 +8,6 @@ def __init__(self, BASE_PATH): self.velocity = pygame.Vector2() self.velocity.xy = 3, 0 self.acceleration = 0.1 - self.rightSprite = pygame.image.load(BASE_PATH + '/data/gfx/player.png') + self.rightSprite = pygame.image.load(BASE_PATH + "/data/gfx/player.png") self.leftSprite = pygame.transform.flip(self.rightSprite, True, False) self.currentSprite = self.rightSprite diff --git a/utils.py b/utils.py index dad03a7..335ffe9 100644 --- a/utils.py +++ b/utils.py @@ -5,5 +5,11 @@ def clamp(value, min, max): return max return value + def checkCollisions(a_x, a_y, a_width, a_height, b_x, b_y, b_width, b_height): - return (a_x + a_width > b_x) and (a_x < b_x + b_width) and (a_y + a_height > b_y) and (a_y < b_y + b_height) + return ( + (a_x + a_width > b_x) + and (a_x < b_x + b_width) + and (a_y + a_height > b_y) + and (a_y < b_y + b_height) + ) From 5be8526e5a22a0d286406dc6c3023a3fef068699 Mon Sep 17 00:00:00 2001 From: Jan Poonthong Date: Wed, 14 Sep 2022 10:19:59 +0700 Subject: [PATCH 6/6] Fix BASE_PATH bug --- background.py | 3 ++- main.py | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/background.py b/background.py index 33b22d0..dbaed73 100644 --- a/background.py +++ b/background.py @@ -1,4 +1,5 @@ -import pygame, colorsys +import colorsys +import pygame class Background: diff --git a/main.py b/main.py index e1e0537..7cbd4de 100644 --- a/main.py +++ b/main.py @@ -1,13 +1,19 @@ -import pygame, sys, time, random, colorsys, math from os.path import abspath, dirname -from pygame.math import Vector2 + +import colorsys +import math +import pygame +import random +import sys +import time from pygame.locals import * -from player import Player + from background import Background -from button import Button from bean import Bean -from utils import clamp +from button import Button +from player import Player from utils import checkCollisions +from utils import clamp def main(): @@ -360,7 +366,7 @@ def main(): oldBeanMultipler = beanMultiplier beanMultiplier += 10 for i in range(beanMultiplier): - beans.append(Bean()) + beans.append(Bean(BASE_PATH)) beans[-1].position.xy = ( random.randrange( 0,