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

0 #51

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/FlapPyBird.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import asyncio
# import asyncio
#
# from src_old.flappy import Flappy
#
# if __name__ == "__main__":
# asyncio.run(Flappy().start())

from src.flappy import Flappy

import src.game

if __name__ == "__main__":
asyncio.run(Flappy().start())
game = src.game.Game()
game.main_loop()
36 changes: 24 additions & 12 deletions src/entities/background.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
from ..utils import GameConfig
from .entity import Entity
from src.utils.manager import Manager
from src.utils.stats import Stats
import pygame
import random

class Background:
def __init__(self, display_surface):
self.display_surface = display_surface
self.stat = Stats().stat
self.base_image = Manager.get_instance().base
self.image = Manager.get_instance().background[random.randint(0, 1)]
self.rect = pygame.Rect(0, 400, 336, 112)

class Background(Entity):
def __init__(self, config: GameConfig) -> None:
super().__init__(
config,
config.images.background,
0,
0,
config.window.width,
config.window.height,
)
def draw(self):
self.stat = Stats().stat
#画背景
self.display_surface.blit(self.image, self.image.get_rect())
#画地面
if self.stat in ["welcome", "gameover"]:
self.display_surface.blit(self.base_image, self.rect)
elif self.stat == "run":
self.rect.move_ip(-5, 0)
self.display_surface.blit(self.base_image, self.rect)
self.display_surface.blit(self.base_image, self.rect.move(336, 0))
if self.rect.x <= -336:
self.rect.x = 0
60 changes: 60 additions & 0 deletions src/entities/bird.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import pygame
import random
from src.utils.manager import Manager
from src.utils.stats import Stats

class Bird(object):
def __init__(self, display_surface, rect):
# 定义一个小鸟类
self.rect_list = rect
self.display_surface = display_surface
self.birdRect = pygame.Rect(65, 50, 32, 24)
self.birdStatus = Manager.get_instance().birds
self.stat = Stats()
self.birdx = 5
self.birdy = 350
self.jump = False
self.jumpSpeed = 0
self.gravity = 0.05
self.dead = False
self.colorBird=random.randint(0,2)
self.v = 0

def birdUpdate(self):
# 定义移动方法
self.v += self.gravity
self.birdy += self.v
self.birdy += self.jumpSpeed
if self.jump:
# 小鸟跳跃状态
self.jumpSpeed += 0.15
self.v = -2.5
self.v += self.jumpSpeed

if self.jumpSpeed >= 0:
self.jumpSpeed = 0
self.jump = False

def createMap(self):
self.birdRect = pygame.Rect(self.birdx, self.birdy, 32, 24)
self.display_surface.blit(self.birdStatus[self.colorBird][0], self.birdRect)
self.display_surface.blit(self.birdStatus[self.colorBird][1], self.birdRect)
self.display_surface.blit(self.birdStatus[self.colorBird][2], self.birdRect)
if not self.dead:
self.birdUpdate()


def checkDead(self, rect):
#检测小鸟是否死亡
self.rect_list = rect

if 0 <= self.birdy <= 512:
self.dead = False
for x in self.rect_list:
if x.colliderect(self.birdRect):
self.dead = True
print('1')

else:
self.dead = True

148 changes: 46 additions & 102 deletions src/entities/pipe.py
Original file line number Diff line number Diff line change
@@ -1,104 +1,48 @@
import pygame
import random
from typing import List
from src.utils.stats import Stats
from src.utils.manager import Manager

#pipe_length = 320
class Pipe:
def __init__(self, display_surface):
self.display_surface = display_surface
self.images_list = Manager.get_instance().pipes
self.image_up = self.images_list[random.randint(0, 1)]
self.image_down = pygame.transform.flip(self.image_up, False, True)

self.x_0 = 200
self.x_1 = self.x_0 + 170
self.y_0 = random.randint(200, 380)
self.y_1 = random.randint(200, 380)

self.rect_0 = pygame.Rect(self.x_0, self.y_0, 52, 320)
self.rect_1 = pygame.Rect(self.x_1, self.y_1, 52, 320)
self.rect_2 = pygame.Rect(self.x_0, (self.y_0 - 500), 52, 320)
self.rect_3 = pygame.Rect(self.x_1, (self.y_1 - 500), 52, 320)
self.rect_list = [self.rect_0, self.rect_1]
self.rect_list_all = [self.rect_0, self.rect_1, self.rect_2, self.rect_3]
def update(self):
self.x_0 -= 2
self.x_1 -= 2
self.rect_0 = pygame.Rect(self.x_0, self.y_0, 52, 320)
self.rect_1 = pygame.Rect(self.x_1, self.y_1, 52, 320)
self.rect_2 = pygame.Rect(self.x_0, (self.y_0 - 500), 52, 320)
self.rect_3 = pygame.Rect(self.x_1, (self.y_1 - 500), 52, 320)
self.rect_list_all = [self.rect_0, self.rect_1, self.rect_2, self.rect_3]
self.rect_list = [self.rect_0, self.rect_1]
if self.x_0 <= -52:
self.x_0 = 288
self.y_0 = random.randint(200, 380)
if self.x_1 <= -52:
self.x_1 = 288
self.y_1 = random.randint(200, 380)


def draw(self):
for x in self.rect_list_all:
if x in self.rect_list:
self.display_surface.blit(self.image_up, x)
else:
self.display_surface.blit(self.image_down, x)

from ..utils import GameConfig
from .entity import Entity


class Pipe(Entity):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.vel_x = -5

def draw(self) -> None:
self.x += self.vel_x
super().draw()


class Pipes(Entity):
upper: List[Pipe]
lower: List[Pipe]

def __init__(self, config: GameConfig) -> None:
super().__init__(config)
self.pipe_gap = 120
self.top = 0
self.bottom = self.config.window.viewport_height
self.upper = []
self.lower = []
self.spawn_initial_pipes()

def tick(self) -> None:
if self.can_spawn_pipes():
self.spawn_new_pipes()
self.remove_old_pipes()

for up_pipe, low_pipe in zip(self.upper, self.lower):
up_pipe.tick()
low_pipe.tick()

def stop(self) -> None:
for pipe in self.upper + self.lower:
pipe.vel_x = 0

def can_spawn_pipes(self) -> bool:
last = self.upper[-1]
if not last:
return True

return self.config.window.width - (last.x + last.w) > last.w * 2.5

def spawn_new_pipes(self):
# add new pipe when first pipe is about to touch left of screen
upper, lower = self.make_random_pipes()
self.upper.append(upper)
self.lower.append(lower)

def remove_old_pipes(self):
# remove first pipe if its out of the screen
for pipe in self.upper:
if pipe.x < -pipe.w:
self.upper.remove(pipe)

for pipe in self.lower:
if pipe.x < -pipe.w:
self.lower.remove(pipe)

def spawn_initial_pipes(self):
upper_1, lower_1 = self.make_random_pipes()
upper_1.x = self.config.window.width + upper_1.w * 3
lower_1.x = self.config.window.width + upper_1.w * 3
self.upper.append(upper_1)
self.lower.append(lower_1)

upper_2, lower_2 = self.make_random_pipes()
upper_2.x = upper_1.x + upper_1.w * 3.5
lower_2.x = upper_1.x + upper_1.w * 3.5
self.upper.append(upper_2)
self.lower.append(lower_2)

def make_random_pipes(self):
"""returns a randomly generated pipe"""
# y of gap between upper and lower pipe
base_y = self.config.window.viewport_height

gap_y = random.randrange(0, int(base_y * 0.6 - self.pipe_gap))
gap_y += int(base_y * 0.2)
pipe_height = self.config.images.pipe[0].get_height()
pipe_x = self.config.window.width + 10

upper_pipe = Pipe(
self.config,
self.config.images.pipe[0],
pipe_x,
gap_y - pipe_height,
)

lower_pipe = Pipe(
self.config,
self.config.images.pipe[1],
pipe_x,
gap_y + self.pipe_gap,
)

return upper_pipe, lower_pipe
21 changes: 21 additions & 0 deletions src/entities/words.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pygame
from src.utils.manager import Manager
import random

class Words:
def __init__(self, display_surface):
self.display_surface = display_surface
self.image_GM = Manager.get_instance().gameover
self.rect_GM = pygame.Rect(48, 235, 192, 42)

self.image_M = Manager.get_instance().message
self.rect_M = pygame.Rect(52, 122.5, 184, 267)
def draw_GM(self):
self.display_surface.blit(self.image_GM, self.rect_GM)


def draw_M(self):
self.display_surface.blit(self.image_M, self.rect_M)



Loading