Skip to content

Commit

Permalink
integrate bull
Browse files Browse the repository at this point in the history
  • Loading branch information
cbdj committed Jul 14, 2023
1 parent 05c166a commit 4f371ea
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 52 deletions.
28 changes: 13 additions & 15 deletions Settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@ def get_platform():

package_domain="com.cldejessey"
package_name="flappy"
try :
base_path = pg.system.get_pref_path(package_domain,package_name)
except:
if platform=="android":
base_path=os.path.abspath(f"/data/data/{package_domain}.{package_name}/files/app/")
elif platform=="win":
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
else:
# try :
# base_path = pg.system.get_pref_path(package_domain,package_name)
# except:
if platform=="android":
base_path=os.path.abspath(f"/data/data/{package_domain}.{package_name}/files/app/")
elif platform=="win":
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
else:
base_path = os.path.abspath(".")
print(f"base path : {base_path}")
base_path = os.path.abspath(".") # A SUPPRIMER
# Those two settings will be identified at app startup depending on screen resolution and assets dimensions
WIN_W = 0
WIN_H = 0
Expand All @@ -54,7 +53,7 @@ def get_platform():
PIPE_DENSITY = 6
BUMP_SPEED = 400
FONT_SIZE = 40
SPEED = 100 # initial pipe and base speed
SPEED = 120 # initial pipe and base speed
speed_multiplier = 1.0 # parameter that can be modified in configuration menu
SPEED_INCREASE_FACTOR = 10 # speed increase at each day/night event
DAY_NIGHT_TIME_MS = 10000
Expand All @@ -66,4 +65,3 @@ def get_platform():
EVENT_SOUND = pg.event.custom_type()
SHAKE_SCREEN = pg.event.custom_type()
INCREMENT_SPEED = pg.event.custom_type()
BULL_TIMEOUT = 10
19 changes: 10 additions & 9 deletions SpriteHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(self, app):
scale = screen_info.current_h/background_sprite_h
Settings.BUMP_SPEED = Settings.BUMP_SPEED * scale
Settings.SPEED = Settings.SPEED * scale
Bull.speed_increment = Bull.speed_increment * scale
Settings.SPEED_INCREASE_FACTOR = Settings.SPEED_INCREASE_FACTOR * scale
Settings.BIRD_MASS_KG = Settings.BIRD_MASS_KG * scale
for key, image in self.images.items():
Expand Down Expand Up @@ -111,10 +112,8 @@ def get_achievements_cb(achievements):
self.pipe_width = self.images['pipe-green'].get_width()
self.pipe_requeue_interval = self.rand_pipe_requeue_interval(self.app.speed)
self.pipe_reque_time = 0.0
self.reset()

def reset(self):
self.day=True
self._started = False
self.background.reset()
self.bull.reset()
Expand All @@ -128,9 +127,8 @@ def reset(self):
self.group_foreground.add(self.menu, self.score, self.leaderboard_button, self.best, self.show_settings_button)
if Settings.platform == 'android':
self.group_foreground.add(self.show_google_play_services_button)
self.update_speed(0)
self.app.dt=0.0
self.app.speed = int(Settings.SPEED*Settings.speed_multiplier)
self.app.speed = 0
self.group_background.update()
self.group_collide.update()
self.group_bonus.update()
Expand All @@ -145,8 +143,8 @@ def rand_pipe_requeue_interval(self, speed):
return uniform(2.5 + offset,7.0 + offset)*self.pipe_width/speed

def update_speed(self, speed):
# print(f"update speed : {speed}")
self.base.update_speed(-speed)
# self.base.vel_x = -speed
self.bird.vel_x = -speed/4
self.bull.vel_x = -speed
for (pipe, pipe_reversed) in self.pipes :
Expand Down Expand Up @@ -192,9 +190,14 @@ def pipe_requeue(pipe, pipe_reversed):
Reque pipes
Used when pipes are out of camera range
"""
factor = randrange(0, 2*pipe.rect.height//3)
vel_x = pipe.vel_x
factor = randrange(0, 2*pipe.orig_rect.height//3)
pipe.reset()
pipe.y = pipe.orig_y - factor
pipe.vel_x = vel_x
pipe_reversed.reset()
pipe_reversed.y = pipe_reversed.orig_y - factor
pipe_reversed.vel_x = vel_x
pipe.x = pipe_reversed.x = Settings.WIN_W + pipe.rect.width/2
def bull_requeue():
"""
Expand All @@ -207,7 +210,6 @@ def bull_requeue():

self.pipe_reque_time += self.app.dt
if self.pipe_reque_time > self.pipe_requeue_interval:

if self.bull.vel_x != 0 and self.bull.x < -self.bull.rect.width and self.bull.ready:
self.pipe_reque_time = 0.0
self.pipe_requeue_interval = self.rand_pipe_requeue_interval(-self.bull.vel_x)
Expand Down Expand Up @@ -313,12 +315,11 @@ def start(self):
if Settings.platform == 'android' :
self.group_foreground.add(self.show_google_play_services_button)
self.app.speed = int(Settings.SPEED*Settings.speed_multiplier)
self.update_speed(self.app.speed)
pg.time.set_timer(Settings.EVENT_DAY_NIGHT, Settings.DAY_NIGHT_TIME_MS)

def stop(self):
self._started = False
self.update_speed(0)
self.app.speed = 0
pg.time.set_timer(Settings.EVENT_DAY_NIGHT, 0)

def game_over(self):
Expand Down
54 changes: 38 additions & 16 deletions Sprites/Bird.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@
class Bird(SpriteUnit):
def __init__(self, handler, image_down, image_middle, image_up, x, y, bump_speed, mass, trainee : pg.Surface):
super().__init__(handler, image_middle, x, y)
self.image_down = Image(Texture.from_surface(self.handler.renderer, image_down))
self.image_middle = Image(Texture.from_surface(self.handler.renderer, image_middle))
self.image_up = Image(Texture.from_surface(self.handler.renderer, image_up))
self._image_down = Image(Texture.from_surface(self.handler.renderer, image_down))
self._image_middle = Image(Texture.from_surface(self.handler.renderer, image_middle))
self._image_up = Image(Texture.from_surface(self.handler.renderer, image_up))
self._angry_down = Image(Texture.from_surface(self.handler.renderer, self.handler.images['angrybird-downflap']))
self._angry_middle = Image(Texture.from_surface(self.handler.renderer, self.handler.images['angrybird-midflap']))
self._angry_up = Image(Texture.from_surface(self.handler.renderer, self.handler.images['angrybird-upflap']))
self.image_down = self._image_down
self.image_middle = self._image_middle
self.image_up = self._image_up
self.image : Image = self.image_middle
self.floor = self.handler.base.rect.y - self.handler.base.rect.h//2 - self.image.get_rect().h//2
self.mass = mass
Expand All @@ -23,7 +29,8 @@ def __init__(self, handler, image_down, image_middle, image_up, x, y, bump_speed
self.bubbles = Bubbles(self.handler.renderer,self.x,self.y, self.handler.base.rect.y - self.handler.base.rect.h//2)
self.trainee = self.smoke
self.bottom = Settings.WIN_H - self.image.get_rect().w//4
self.bull = False
self._bull = False
self._collided = False

def translate(self):
if self.x < 2*self.orig_x/3:
Expand Down Expand Up @@ -62,27 +69,41 @@ def bull(self):
def bull(self, value:bool):
self._bull = value
if value:
pg.time.set_timer(Bull.event, Bull.timeout)
pg.event.post(pg.event.Event(Settings.INCREMENT_SPEED, increment = -Bull.speed_increment))
self.image_down = self._angry_down
self.image_middle = self._angry_middle
self.image_up = self._angry_up
pg.time.set_timer(pg.event.Event(Bull.event,phase=1), Bull.timeout)
else:
pg.event.post(pg.event.Event(Settings.INCREMENT_SPEED, increment = Bull.speed_increment))
self.image_down = self._image_down
self.image_middle = self._image_middle
self.image_up = self._image_up

def update(self):
self.translate()
bonus = pg.sprite.spritecollide(self, self.handler.group_bonus,dokill=False)
bonus = pg.sprite.spritecollide(self, self.handler.group_bonus,dokill=True,collided=pg.sprite.collide_mask)
if len(bonus):
for bonu in bonus:
if isinstance(bonu, Bull) and not self.bull:
pg.event.post(pg.event.Event(Settings.EVENT_SOUND, sound = 'roar'))
self.bull = True
pg.event.post(pg.event.Event(Settings.INCREMENT_SPEED, increment = Bull.speed_increment))
bonu.ready = False

if not self.dead and not self.hit and pg.sprite.spritecollideany(self, self.handler.group_collide, pg.sprite.collide_mask):
self.hit = True
self.vel_x = 0
self.handler.update_speed(0)
pg.event.post(pg.event.Event(Settings.EVENT_SOUND, sound = 'hit'))
colliding = pg.sprite.spritecollideany(self, self.handler.group_collide, pg.sprite.collide_mask)
if colliding is None:
self._collided = False
if not self.dead and not self.hit and not self._collided and colliding is not None:
self._collided = True
if not self.bull:
self.hit = True
self.vel_x = 0
self.handler.app.speed = 0
pg.event.post(pg.event.Event(Settings.EVENT_SOUND, sound = 'hit'))
if self.y < self.floor:
pg.event.post(pg.event.Event(Settings.EVENT_SOUND, sound = 'die'))
else:
pg.event.post(pg.event.Event(Settings.EVENT_SOUND, sound = 'explosion'))
colliding.smash(self.y)
pg.event.post(pg.event.Event(Settings.SHAKE_SCREEN, duration = 20, intensity = 10))
if self.y < self.floor:
pg.event.post(pg.event.Event(Settings.EVENT_SOUND, sound = 'die'))

if self.y > self.floor:
self.trainee = self.bubbles
Expand Down Expand Up @@ -113,6 +134,7 @@ def draw(self):

def reset(self):
super().reset()
self.bull = False
self.dead=False
self.hit=False
self.weight = 0.0
Expand Down
17 changes: 13 additions & 4 deletions Sprites/Bull.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,34 @@
import Settings
from Particles.Smoke import Smoke
from Particles.Bubbles import Bubbles
import random

class Bull(SpriteUnit):
timeout = 10
timeout = 6000
event = pg.event.custom_type()
speed_increment = 30
speed_increment = 300
gamble = 8 # minimum 1, the greater, the less chance to hit
def __init__(self, handler, image, x, y):
super().__init__(handler, image, x, y)
self.height = image.get_height()
self._ready = True
self._ready = False
pg.time.set_timer(pg.event.Event(Bull.event,phase=0), Bull.timeout)

def translate(self):
self.x += self.vel_x * self.handler.app.dt
self.rect.center = self.x, self.y
self.rect.center = self.x+random.randint(-2, 2), self.y+random.randint(-2, 2)

def update(self):
x=self.x
self.translate()
if self.x < 0 and x > 0:
self.ready = False
pg.time.set_timer(pg.event.Event(Bull.event,phase=0), Bull.timeout)

def reset(self):
super().reset()
self._ready = False
pg.time.set_timer(pg.event.Event(Bull.event,phase=0), Bull.timeout)

@property
def ready(self):
Expand Down
18 changes: 16 additions & 2 deletions Sprites/Pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@ class Pipe(SpriteUnit):
def __init__(self, handler, image, x, y):
super().__init__(handler, image, x, y)
self.point_given = True
self.rotate_speed = 0
self.shrink_factor = 1.0

def translate(self):
self.x += self.vel_x * self.handler.app.dt
self.rect.center = self.x, self.y

self.image.angle += self.rotate_speed * self.handler.app.dt
self.rect.w *= self.shrink_factor
self.rect.h *= self.shrink_factor

def smash(self, point):
if point > self.y:
self.rotate_speed = self.vel_x
else:
self.rotate_speed = -self.vel_x
self.shrink_factor = 0.99

def reset(self):
super().reset()
self.point_given = True
self.point_given = True
self.rotate_speed = 0
self.shrink_factor = 1.0
1 change: 1 addition & 0 deletions Sprites/SpriteUnit.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def update_image(self, image : Image):
self.rect.center = self.x, self.y

def reset(self):
self.rect = self.orig_rect.copy()
self.x = self.orig_x
self.y = self.orig_y
self.vel_x = 0
Expand Down
Binary file added assets/audio/explosion.mp3
Binary file not shown.
Binary file added assets/audio/roar.mp3
Binary file not shown.
Binary file added assets/sprites/angrybird-downflap.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/sprites/angrybird-midflap.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/sprites/angrybird-upflap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"sound": true, "dark_mode": false, "show_fps": false, "speed_multiplier": 1.0}
37 changes: 31 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self):
except:
print("Couldn't init mixer : no sound")
self.dt = 0.0
self.speed = Settings.SPEED
self._speed = Settings.SPEED
screen_info = pg.display.Info()
size=(screen_info.current_w,screen_info.current_h)
self.window = Window(title='flap.py', size=size, fullscreen=Settings.FULLSCREEN)
Expand All @@ -51,9 +51,20 @@ def __init__(self):

self.shake_intensity = 0
self.shake_duration = 0
self.sprite_handler.reset()
if Settings.platform=='android':
loadingscreen.hide_loading_screen()


self._bull_gamble = Bull.gamble

@property
def speed(self):
return self._speed
@speed.setter
def speed(self, value):
self._speed = value
self.sprite_handler.update_speed(self._speed)

def set_dark_mode(self,on):
self.sprite_handler.set_dark_mode(on)

Expand Down Expand Up @@ -142,12 +153,26 @@ def check_events(self):
elif e.type == Settings.SHAKE_SCREEN:
self.shake_intensity = e.intensity
self.shake_duration = e.duration
elif e.type == Settings.INCREMENT_SPEED:
elif e.type == Settings.INCREMENT_SPEED and self.speed !=0:
self.speed += e.increment
self.sprite_handler.update_speed(self.speed)
elif e.type == Bull.event:
self.sprite_handler.bird.bull = False
self.sprite_handler.bull.ready = True
if e.phase==0:
if random.randint(1,self._bull_gamble) == 1:
pg.time.set_timer(Bull.event, 0)
self._bull_gamble = Bull.gamble
self.sprite_handler.bull.ready = True
self.sprite_handler.group_bonus.add(self.sprite_handler.bull)
# print('bull gamble succeed')
else:
self._bull_gamble -= 1
# print('bull gamble failed')
if e.phase==1:
pg.event.post(pg.event.Event(Settings.INCREMENT_SPEED, increment = -Bull.speed_increment))
pg.time.set_timer(pg.event.Event(Bull.event,phase=2), Bull.timeout//10)
elif e.phase==2:
self.sprite_handler.bird.bull = False
self.sprite_handler.bull.x = self.sprite_handler.bull.orig_x
pg.time.set_timer(pg.event.Event(Bull.event,phase=0), Bull.timeout)

def run(self):
while self.running:
Expand Down

0 comments on commit 4f371ea

Please sign in to comment.