Skip to content
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

Make camera stop on death #45

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
2 changes: 1 addition & 1 deletion background.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pygame
import pygame, colorsys
class Background:
def __init__(self):
self.sprite = pygame.image.load('data/gfx/bg.png')
Expand Down
129 changes: 69 additions & 60 deletions main.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -137,59 +137,67 @@ 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


if not dead: # Only update the camera offset if the player is alive
camOffset = -player.position.y + DISPLAY.get_height() / 2 - player.currentSprite.get_size()[1] / 2

DISPLAY.fill(WHITE)

# Render the background
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(
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
Expand All @@ -201,35 +209,37 @@ 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)

health -= 0.2*dt
player.position.y += player.velocity.y * dt
player.velocity.y = clamp(player.velocity.y + player.acceleration * dt, -99999999999, 50)
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.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())):
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())

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()):
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)
button.price = round(button.price * 2.5)
if (buttons.index(button) == 0):
flapForce *= 1.5
if (buttons.index(button) == 1):
Expand All @@ -240,8 +250,9 @@ def main():
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()):

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
Expand All @@ -251,25 +262,23 @@ def main():
flapForce = 3
beanMultiplier = 5
buttons = []
for i in range(3): buttons.append(Button())
for i in range(3):
buttons.append(Button())
buttons[0].typeIndicatorSprite = pygame.image.load('data/gfx/flap_indicator.png')
buttons[0].price = 5
buttons[0].price = 5
buttons[1].typeIndicatorSprite = pygame.image.load('data/gfx/speed_indicator.png')
buttons[1].price = 5
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())
for i in range(5):
beans.append(Bean())
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


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()

dead = False

pygame.display.update()
pygame.time.delay(10)

Expand Down