From ff6c0b9818e3973fad419ce82675d9fb34ce53c6 Mon Sep 17 00:00:00 2001 From: 01Researcher <13944016111@163.com> Date: Sat, 23 Nov 2024 14:14:22 +0800 Subject: [PATCH 01/15] cleanup --- main.py | 2 +- {src => src_old}/__init__.py | 0 {src => src_old}/entities/__init__.py | 0 {src => src_old}/entities/background.py | 0 {src => src_old}/entities/entity.py | 0 {src => src_old}/entities/floor.py | 0 {src => src_old}/entities/game_over.py | 0 {src => src_old}/entities/pipe.py | 0 {src => src_old}/entities/player.py | 0 {src => src_old}/entities/score.py | 0 {src => src_old}/entities/welcome_message.py | 0 {src => src_old}/flappy.py | 0 {src => src_old}/utils/__init__.py | 0 {src => src_old}/utils/constants.py | 0 {src => src_old}/utils/game_config.py | 0 {src => src_old}/utils/images.py | 0 {src => src_old}/utils/sounds.py | 0 {src => src_old}/utils/utils.py | 0 {src => src_old}/utils/window.py | 0 19 files changed, 1 insertion(+), 1 deletion(-) rename {src => src_old}/__init__.py (100%) rename {src => src_old}/entities/__init__.py (100%) rename {src => src_old}/entities/background.py (100%) rename {src => src_old}/entities/entity.py (100%) rename {src => src_old}/entities/floor.py (100%) rename {src => src_old}/entities/game_over.py (100%) rename {src => src_old}/entities/pipe.py (100%) rename {src => src_old}/entities/player.py (100%) rename {src => src_old}/entities/score.py (100%) rename {src => src_old}/entities/welcome_message.py (100%) rename {src => src_old}/flappy.py (100%) rename {src => src_old}/utils/__init__.py (100%) rename {src => src_old}/utils/constants.py (100%) rename {src => src_old}/utils/game_config.py (100%) rename {src => src_old}/utils/images.py (100%) rename {src => src_old}/utils/sounds.py (100%) rename {src => src_old}/utils/utils.py (100%) rename {src => src_old}/utils/window.py (100%) diff --git a/main.py b/main.py index afbf6ae8..d9251415 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ import asyncio -from src.flappy import Flappy +from src_old.flappy import Flappy if __name__ == "__main__": asyncio.run(Flappy().start()) diff --git a/src/__init__.py b/src_old/__init__.py similarity index 100% rename from src/__init__.py rename to src_old/__init__.py diff --git a/src/entities/__init__.py b/src_old/entities/__init__.py similarity index 100% rename from src/entities/__init__.py rename to src_old/entities/__init__.py diff --git a/src/entities/background.py b/src_old/entities/background.py similarity index 100% rename from src/entities/background.py rename to src_old/entities/background.py diff --git a/src/entities/entity.py b/src_old/entities/entity.py similarity index 100% rename from src/entities/entity.py rename to src_old/entities/entity.py diff --git a/src/entities/floor.py b/src_old/entities/floor.py similarity index 100% rename from src/entities/floor.py rename to src_old/entities/floor.py diff --git a/src/entities/game_over.py b/src_old/entities/game_over.py similarity index 100% rename from src/entities/game_over.py rename to src_old/entities/game_over.py diff --git a/src/entities/pipe.py b/src_old/entities/pipe.py similarity index 100% rename from src/entities/pipe.py rename to src_old/entities/pipe.py diff --git a/src/entities/player.py b/src_old/entities/player.py similarity index 100% rename from src/entities/player.py rename to src_old/entities/player.py diff --git a/src/entities/score.py b/src_old/entities/score.py similarity index 100% rename from src/entities/score.py rename to src_old/entities/score.py diff --git a/src/entities/welcome_message.py b/src_old/entities/welcome_message.py similarity index 100% rename from src/entities/welcome_message.py rename to src_old/entities/welcome_message.py diff --git a/src/flappy.py b/src_old/flappy.py similarity index 100% rename from src/flappy.py rename to src_old/flappy.py diff --git a/src/utils/__init__.py b/src_old/utils/__init__.py similarity index 100% rename from src/utils/__init__.py rename to src_old/utils/__init__.py diff --git a/src/utils/constants.py b/src_old/utils/constants.py similarity index 100% rename from src/utils/constants.py rename to src_old/utils/constants.py diff --git a/src/utils/game_config.py b/src_old/utils/game_config.py similarity index 100% rename from src/utils/game_config.py rename to src_old/utils/game_config.py diff --git a/src/utils/images.py b/src_old/utils/images.py similarity index 100% rename from src/utils/images.py rename to src_old/utils/images.py diff --git a/src/utils/sounds.py b/src_old/utils/sounds.py similarity index 100% rename from src/utils/sounds.py rename to src_old/utils/sounds.py diff --git a/src/utils/utils.py b/src_old/utils/utils.py similarity index 100% rename from src/utils/utils.py rename to src_old/utils/utils.py diff --git a/src/utils/window.py b/src_old/utils/window.py similarity index 100% rename from src/utils/window.py rename to src_old/utils/window.py From 868afcf9c89766fd8afac37dc0385f41f04b3fbd Mon Sep 17 00:00:00 2001 From: 01Researcher <13944016111@163.com> Date: Sat, 23 Nov 2024 14:42:36 +0800 Subject: [PATCH 02/15] screen.py --- main.py | 5 +++-- src/main.py | 35 +++++++++++++++++++++++++++++++++++ src/utils/manager.py | 0 src/utils/screen.py | 7 +++++++ src/utils/settings.py | 5 +++++ 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/main.py create mode 100644 src/utils/manager.py create mode 100644 src/utils/screen.py create mode 100644 src/utils/settings.py diff --git a/main.py b/main.py index d9251415..9479e99e 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ import asyncio -from src_old.flappy import Flappy +import src.main if __name__ == "__main__": - asyncio.run(Flappy().start()) + game = src.main.Game() + game.main_loop() diff --git a/src/main.py b/src/main.py new file mode 100644 index 00000000..c27e6d7e --- /dev/null +++ b/src/main.py @@ -0,0 +1,35 @@ +import sys + +import pygame + +from src.utils.screen import Screen +from src.utils.settings import Settings + + +class Game: + def __init__(self): + pygame.init() + pygame.display.set_caption("Flappy Bird") + self.settings = Settings() + self.screen = Screen(pygame.display.set_mode(self.settings.window_size)) + self.clock = pygame.time.Clock() + + def handle_events(self): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + sys.exit() + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_q: + pygame.quit() + sys.exit() + + def main_loop(self): + while True: + self.handle_events() + pygame.display.flip() + self.clock.tick(self.settings.fps) + + + def initialize(self): + pass diff --git a/src/utils/manager.py b/src/utils/manager.py new file mode 100644 index 00000000..e69de29b diff --git a/src/utils/screen.py b/src/utils/screen.py new file mode 100644 index 00000000..05bcb294 --- /dev/null +++ b/src/utils/screen.py @@ -0,0 +1,7 @@ +class Screen: + def __init__(self, screen): + self.screen = screen + + def flush(self, sprites: list): + for s in sprites: + s.blit(self.screen) diff --git a/src/utils/settings.py b/src/utils/settings.py new file mode 100644 index 00000000..94c1d2dc --- /dev/null +++ b/src/utils/settings.py @@ -0,0 +1,5 @@ +class Settings: + def __init__(self, window_size=(288, 512), fps=30): + self.window_size = window_size + self.fps = fps + From 61effad406bc79cbbd69adbb697ccb6b39d88b53 Mon Sep 17 00:00:00 2001 From: 01Researcher <13944016111@163.com> Date: Sat, 23 Nov 2024 15:06:14 +0800 Subject: [PATCH 03/15] screen.py --- src/utils/manager.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/utils/manager.py b/src/utils/manager.py index e69de29b..47e0789a 100644 --- a/src/utils/manager.py +++ b/src/utils/manager.py @@ -0,0 +1,20 @@ +import pygame.image +import os + + +FILE_ROOT = r"F:\projects\FlapPyBird\assets\sprites" + + +class Manager: + # num0 = pygame.image.load(FILE_ROOT + "0.png") + + def __init__(self): + self.assets_dictionary = {} + for path, folders, files in os.walk(FILE_ROOT): + print(files) + for f in files: + k = f.split(".")[0] + self.assets_dictionary[k] = pygame.image.load(os.path.join(path, f)) + + +Manager() \ No newline at end of file From 37484c2ffe57e1335c29feb60849f88871d992e3 Mon Sep 17 00:00:00 2001 From: 01Researcher <13944016111@163.com> Date: Sat, 23 Nov 2024 16:00:12 +0800 Subject: [PATCH 04/15] Background --- src/entities/background.py | 11 +++++++++++ src/main.py | 17 ++++++++++++++--- src/utils/manager.py | 4 ++-- 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 src/entities/background.py diff --git a/src/entities/background.py b/src/entities/background.py new file mode 100644 index 00000000..c6b6400b --- /dev/null +++ b/src/entities/background.py @@ -0,0 +1,11 @@ +from src.utils.manager import Manager +import pygame + +class Background: + def __init__(self, display_surface): + self.display_surface = display_surface + self.manager = Manager() + self.image = pygame.image.load(r"F:\projects\FlapPyBird\assets\sprites\background-day.png").convert() + self.rect = self.image.get_rect() + def draw(self): + self.display_surface.blit(self.image, self.rect) \ No newline at end of file diff --git a/src/main.py b/src/main.py index c27e6d7e..9c13244a 100644 --- a/src/main.py +++ b/src/main.py @@ -2,8 +2,10 @@ import pygame -from src.utils.screen import Screen +#from src.utils.screen import Screen from src.utils.settings import Settings +from src.utils.manager import Manager +from src.entities.background import Background class Game: @@ -11,8 +13,11 @@ def __init__(self): pygame.init() pygame.display.set_caption("Flappy Bird") self.settings = Settings() - self.screen = Screen(pygame.display.set_mode(self.settings.window_size)) + self.screen = pygame.display.set_mode(self.settings.window_size)#改成Screen() self.clock = pygame.time.Clock() + self.manager = Manager() + + self.background = Background(self.screen) def handle_events(self): for event in pygame.event.get(): @@ -24,12 +29,18 @@ def handle_events(self): pygame.quit() sys.exit() + def draw_background(self): + self.background.draw() + + def main_loop(self): while True: self.handle_events() - pygame.display.flip() + self.draw_background() self.clock.tick(self.settings.fps) + pygame.display.flip() + def initialize(self): pass diff --git a/src/utils/manager.py b/src/utils/manager.py index 47e0789a..89885ae8 100644 --- a/src/utils/manager.py +++ b/src/utils/manager.py @@ -15,6 +15,6 @@ def __init__(self): for f in files: k = f.split(".")[0] self.assets_dictionary[k] = pygame.image.load(os.path.join(path, f)) + # print(self.assets_dictionary) - -Manager() \ No newline at end of file +Manager() From 2ec31f8bb7f513b99f98ac3a8e0acacdbfc34168 Mon Sep 17 00:00:00 2001 From: 01Researcher <13944016111@163.com> Date: Sat, 23 Nov 2024 16:46:42 +0800 Subject: [PATCH 05/15] 11-23 --- .idea/.gitignore | 3 ++ .idea/FlapPyBird.iml | 12 ++++++++ .../inspectionProfiles/profiles_settings.xml | 6 ++++ .idea/misc.xml | 7 +++++ .idea/modules.xml | 8 ++++++ .idea/vcs.xml | 6 ++++ main.py | 5 ++-- src/entities/background.py | 2 +- src/entities/words.py | 19 +++++++++++++ src/main.py | 28 +++++++++++++++++-- src_old/entities/entity.py | 2 +- 11 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/FlapPyBird.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 src/entities/words.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..359bb530 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml diff --git a/.idea/FlapPyBird.iml b/.idea/FlapPyBird.iml new file mode 100644 index 00000000..8b8c3954 --- /dev/null +++ b/.idea/FlapPyBird.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..105ce2da --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..a6218fed --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..300b0a47 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/main.py b/main.py index 9479e99e..d9251415 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,6 @@ import asyncio -import src.main +from src_old.flappy import Flappy if __name__ == "__main__": - game = src.main.Game() - game.main_loop() + asyncio.run(Flappy().start()) diff --git a/src/entities/background.py b/src/entities/background.py index c6b6400b..ab40e3a2 100644 --- a/src/entities/background.py +++ b/src/entities/background.py @@ -5,7 +5,7 @@ class Background: def __init__(self, display_surface): self.display_surface = display_surface self.manager = Manager() - self.image = pygame.image.load(r"F:\projects\FlapPyBird\assets\sprites\background-day.png").convert() + self.image = pygame.image.load(r"F:\projects\FlapPyBird\assets\sprites\background-night.png").convert() self.rect = self.image.get_rect() def draw(self): self.display_surface.blit(self.image, self.rect) \ No newline at end of file diff --git a/src/entities/words.py b/src/entities/words.py new file mode 100644 index 00000000..6324bd5f --- /dev/null +++ b/src/entities/words.py @@ -0,0 +1,19 @@ +import pygame +class Words: + def __init__(self, display_surface): + self.display_surface = display_surface + + self.image_GM = pygame.image.load(r"F:\projects\FlapPyBird\assets\sprites\gameover.png").convert().convert_alpha() + self.rect_GM = pygame.Rect(48, 235, 192, 42) + + self.image_M = pygame.image.load(r"F:\projects\FlapPyBird\assets\sprites\message.png").convert().convert_alpha() + 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) + + + diff --git a/src/main.py b/src/main.py index 9c13244a..1b050e6b 100644 --- a/src/main.py +++ b/src/main.py @@ -6,6 +6,7 @@ from src.utils.settings import Settings from src.utils.manager import Manager from src.entities.background import Background +from src.entities.words import Words class Game: @@ -18,6 +19,9 @@ def __init__(self): self.manager = Manager() self.background = Background(self.screen) + self.words = Words(self.background.image) + + self.stats = 0 #0=welcome,1=run,2=gameover def handle_events(self): for event in pygame.event.get(): @@ -28,18 +32,36 @@ def handle_events(self): if event.key == pygame.K_q: pygame.quit() sys.exit() + elif event.type == pygame.MOUSEBUTTONDOWN: + #响应鼠标点击 + pass - def draw_background(self): + def update_screen(self): self.background.draw() + pygame.display.flip() + if self.stats == 0: + self.words.draw_M() + pass + #画开始界面 + elif self.stats == 1: + pass + # 画游戏界面的物品 + elif self.stats == 2: + self.words.draw_GM() + + pass + #画gameover + def main_loop(self): while True: self.handle_events() - self.draw_background() + self.update_screen() + self.clock.tick(self.settings.fps) - pygame.display.flip() + def initialize(self): diff --git a/src_old/entities/entity.py b/src_old/entities/entity.py index 6ec067ce..673d4962 100644 --- a/src_old/entities/entity.py +++ b/src_old/entities/entity.py @@ -63,7 +63,7 @@ def tick(self) -> None: rect = self.rect if self.config.debug: pygame.draw.rect(self.config.screen, (255, 0, 0), rect, 1) - # write x and y at top of rect + # write x and y at top of rect_GM font = pygame.font.SysFont("Arial", 13, True) text = font.render( f"{self.x:.1f}, {self.y:.1f}, {self.w:.1f}, {self.h:.1f}", From 92afc3f1f3f706b4823087ba7cf4a73d6593bf1c Mon Sep 17 00:00:00 2001 From: 01Researcher <13944016111@163.com> Date: Sat, 30 Nov 2024 14:40:34 +0800 Subject: [PATCH 06/15] 11-30,background.py --- .idea/FlapPyBird.iml | 4 +++- .idea/misc.xml | 2 +- main.py | 13 ++++++++++--- src/entities/background.py | 9 +++++---- src/entities/words.py | 4 ++-- src/{main.py => game.py} | 11 +++++++---- src/utils/manager.py | 34 ++++++++++++++++++++++++++-------- src/utils/stats.py | 4 ++++ 8 files changed, 58 insertions(+), 23 deletions(-) rename src/{main.py => game.py} (85%) create mode 100644 src/utils/stats.py diff --git a/.idea/FlapPyBird.iml b/.idea/FlapPyBird.iml index 8b8c3954..7a6134d1 100644 --- a/.idea/FlapPyBird.iml +++ b/.idea/FlapPyBird.iml @@ -1,7 +1,9 @@ - + + + diff --git a/.idea/misc.xml b/.idea/misc.xml index a6218fed..7f3497b4 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,5 +3,5 @@ - + \ No newline at end of file diff --git a/main.py b/main.py index d9251415..c10b5860 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,13 @@ -import asyncio +# import asyncio +# +# from src_old.flappy import Flappy +# +# if __name__ == "__main__": +# asyncio.run(Flappy().start()) -from src_old.flappy import Flappy + +import src.game if __name__ == "__main__": - asyncio.run(Flappy().start()) + game = src.game.Game() + game.main_loop() diff --git a/src/entities/background.py b/src/entities/background.py index ab40e3a2..9d7c96a1 100644 --- a/src/entities/background.py +++ b/src/entities/background.py @@ -1,11 +1,12 @@ from src.utils.manager import Manager -import pygame +import random +# from src.utils.stats import Stats class Background: def __init__(self, display_surface): self.display_surface = display_surface self.manager = Manager() - self.image = pygame.image.load(r"F:\projects\FlapPyBird\assets\sprites\background-night.png").convert() - self.rect = self.image.get_rect() + self.image = self.manager.background[random.randint(0, 1)] + def draw(self): - self.display_surface.blit(self.image, self.rect) \ No newline at end of file + self.display_surface.blit(self.image, self.image.get_rect()) diff --git a/src/entities/words.py b/src/entities/words.py index 6324bd5f..e85a4538 100644 --- a/src/entities/words.py +++ b/src/entities/words.py @@ -3,10 +3,10 @@ class Words: def __init__(self, display_surface): self.display_surface = display_surface - self.image_GM = pygame.image.load(r"F:\projects\FlapPyBird\assets\sprites\gameover.png").convert().convert_alpha() + self.image_GM = pygame.image.load(r"F:\projects\FlapPyBird\assets\sprites\gameover.png").convert_alpha() self.rect_GM = pygame.Rect(48, 235, 192, 42) - self.image_M = pygame.image.load(r"F:\projects\FlapPyBird\assets\sprites\message.png").convert().convert_alpha() + self.image_M = pygame.image.load(r"F:\projects\FlapPyBird\assets\sprites\message.png").convert_alpha() self.rect_M = pygame.Rect(52, 122.5, 184, 267) def draw_GM(self): self.display_surface.blit(self.image_GM, self.rect_GM) diff --git a/src/main.py b/src/game.py similarity index 85% rename from src/main.py rename to src/game.py index 1b050e6b..338436fb 100644 --- a/src/main.py +++ b/src/game.py @@ -7,6 +7,7 @@ from src.utils.manager import Manager from src.entities.background import Background from src.entities.words import Words +from src.utils.stats import Stats class Game: @@ -17,11 +18,13 @@ def __init__(self): self.screen = pygame.display.set_mode(self.settings.window_size)#改成Screen() self.clock = pygame.time.Clock() self.manager = Manager() + self.stats = Stats() + self.stat = self.stats.stat self.background = Background(self.screen) self.words = Words(self.background.image) - self.stats = 0 #0=welcome,1=run,2=gameover + #self.stats = "gameover" #0=welcome,1=run,2=gameover def handle_events(self): for event in pygame.event.get(): @@ -40,14 +43,14 @@ def update_screen(self): self.background.draw() pygame.display.flip() - if self.stats == 0: + if self.stat == "welcome": self.words.draw_M() pass #画开始界面 - elif self.stats == 1: + elif self.stat == "run": pass # 画游戏界面的物品 - elif self.stats == 2: + elif self.stat == "gameover": self.words.draw_GM() pass diff --git a/src/utils/manager.py b/src/utils/manager.py index 89885ae8..1e6f653b 100644 --- a/src/utils/manager.py +++ b/src/utils/manager.py @@ -2,19 +2,37 @@ import os -FILE_ROOT = r"F:\projects\FlapPyBird\assets\sprites" +FILE_ROOT = "F:\\projects\\FlapPyBird\\assets\\sprites\\" class Manager: # num0 = pygame.image.load(FILE_ROOT + "0.png") def __init__(self): - self.assets_dictionary = {} - for path, folders, files in os.walk(FILE_ROOT): - print(files) - for f in files: - k = f.split(".")[0] - self.assets_dictionary[k] = pygame.image.load(os.path.join(path, f)) - # print(self.assets_dictionary) + pygame.init() + pygame.display.set_mode((288, 512)) + + blue_bird_name = ["bluebird-downflap", "bluebird-midflap", "bluebird-upflap"] + background_name = ["background-day", "background-night"] + + self.blue_bird = [pygame.image.load(image.join([FILE_ROOT, ".png"])).convert_alpha() + for image in blue_bird_name] + + self.background = [pygame.image.load(image.join([FILE_ROOT, ".png"])).convert_alpha() + for image in background_name] + + print(self.blue_bird) + + + + + + # self.assets_dictionary = {} + # for path, folders, files in os.walk(FILE_ROOT): + # print(files) + # for f in files: + # k = f.split(".")[0] + # self.assets_dictionary[k] = pygame.image.load(os.path.join(path, f)) + # # print(self.assets_dictionary) Manager() diff --git a/src/utils/stats.py b/src/utils/stats.py new file mode 100644 index 00000000..e405a27d --- /dev/null +++ b/src/utils/stats.py @@ -0,0 +1,4 @@ +class Stats: + stat_list = ["welcome", "run", "gameover"] + def __init__(self): + self.stat = self.stat_list[0] From 9a2439a3c0325ad57a9c1593c1d34dc72622c30c Mon Sep 17 00:00:00 2001 From: 01Researcher <13944016111@163.com> Date: Sat, 30 Nov 2024 14:55:24 +0800 Subject: [PATCH 07/15] =?UTF-8?q?11-30,=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/entities/background.py | 1 - src/entities/bird.py | 2 ++ src/entities/words.py | 3 +++ src/utils/manager.py | 3 ++- 4 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 src/entities/bird.py diff --git a/src/entities/background.py b/src/entities/background.py index 9d7c96a1..9c13a7c1 100644 --- a/src/entities/background.py +++ b/src/entities/background.py @@ -1,6 +1,5 @@ from src.utils.manager import Manager import random -# from src.utils.stats import Stats class Background: def __init__(self, display_surface): diff --git a/src/entities/bird.py b/src/entities/bird.py new file mode 100644 index 00000000..8e761458 --- /dev/null +++ b/src/entities/bird.py @@ -0,0 +1,2 @@ +class Bird: + def __init__(self): diff --git a/src/entities/words.py b/src/entities/words.py index e85a4538..cdf353f9 100644 --- a/src/entities/words.py +++ b/src/entities/words.py @@ -1,4 +1,7 @@ import pygame +from src.utils.manager import Manager +import random + class Words: def __init__(self, display_surface): self.display_surface = display_surface diff --git a/src/utils/manager.py b/src/utils/manager.py index 1e6f653b..4c197f8c 100644 --- a/src/utils/manager.py +++ b/src/utils/manager.py @@ -21,7 +21,8 @@ def __init__(self): self.background = [pygame.image.load(image.join([FILE_ROOT, ".png"])).convert_alpha() for image in background_name] - print(self.blue_bird) + self.gameover = pygame.image.load() + From b40df62812eb5699f6a48045bcfac6df389c13d2 Mon Sep 17 00:00:00 2001 From: 01Researcher <13944016111@163.com> Date: Sat, 30 Nov 2024 15:57:44 +0800 Subject: [PATCH 08/15] =?UTF-8?q?11-30,=E6=94=B9=E6=88=90=E4=BA=86?= =?UTF-8?q?=E7=9B=B8=E5=AF=B9=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/entities/background.py | 3 +-- src/entities/words.py | 6 +++--- src/utils/manager.py | 8 ++++---- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/entities/background.py b/src/entities/background.py index 9c13a7c1..60704d99 100644 --- a/src/entities/background.py +++ b/src/entities/background.py @@ -4,8 +4,7 @@ class Background: def __init__(self, display_surface): self.display_surface = display_surface - self.manager = Manager() - self.image = self.manager.background[random.randint(0, 1)] + self.image = Manager().background[random.randint(0, 1)] def draw(self): self.display_surface.blit(self.image, self.image.get_rect()) diff --git a/src/entities/words.py b/src/entities/words.py index cdf353f9..e3fc5fdf 100644 --- a/src/entities/words.py +++ b/src/entities/words.py @@ -5,11 +5,11 @@ class Words: def __init__(self, display_surface): self.display_surface = display_surface - - self.image_GM = pygame.image.load(r"F:\projects\FlapPyBird\assets\sprites\gameover.png").convert_alpha() + self.manager = Manager() + self.image_GM = self.manager.gameover self.rect_GM = pygame.Rect(48, 235, 192, 42) - self.image_M = pygame.image.load(r"F:\projects\FlapPyBird\assets\sprites\message.png").convert_alpha() + self.image_M = self.manager.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) diff --git a/src/utils/manager.py b/src/utils/manager.py index 4c197f8c..76c40049 100644 --- a/src/utils/manager.py +++ b/src/utils/manager.py @@ -2,8 +2,8 @@ import os -FILE_ROOT = "F:\\projects\\FlapPyBird\\assets\\sprites\\" - +# FILE_ROOT = "F:\\projects\\FlapPyBird\\assets\\sprites\\" +FILE_ROOT = f"{os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))}/assets/sprites/" class Manager: # num0 = pygame.image.load(FILE_ROOT + "0.png") @@ -21,8 +21,8 @@ def __init__(self): self.background = [pygame.image.load(image.join([FILE_ROOT, ".png"])).convert_alpha() for image in background_name] - self.gameover = pygame.image.load() - + self.gameover = pygame.image.load(os.path.join(FILE_ROOT, "gameover.png")).convert_alpha() + self.message = pygame.image.load(os.path.join(FILE_ROOT, "message.png")).convert_alpha() From 36bd0134af65299a655b9e83901f6d47ff4a5e03 Mon Sep 17 00:00:00 2001 From: 01Researcher <13944016111@163.com> Date: Sat, 30 Nov 2024 16:08:52 +0800 Subject: [PATCH 09/15] =?UTF-8?q?11-30,=E6=94=B9=E6=88=90=E4=BA=86?= =?UTF-8?q?=E7=9B=B8=E5=AF=B9=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/manager.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/utils/manager.py b/src/utils/manager.py index 76c40049..5345b871 100644 --- a/src/utils/manager.py +++ b/src/utils/manager.py @@ -1,8 +1,6 @@ import pygame.image import os - -# FILE_ROOT = "F:\\projects\\FlapPyBird\\assets\\sprites\\" FILE_ROOT = f"{os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))}/assets/sprites/" class Manager: From 3271b76a0787d0c28bb08f437a8f3922ad8a534c Mon Sep 17 00:00:00 2001 From: 01Researcher <13944016111@163.com> Date: Sat, 30 Nov 2024 16:41:54 +0800 Subject: [PATCH 10/15] 11-30 --- src/utils/manager.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/utils/manager.py b/src/utils/manager.py index 5345b871..61d0d564 100644 --- a/src/utils/manager.py +++ b/src/utils/manager.py @@ -10,11 +10,18 @@ def __init__(self): pygame.init() pygame.display.set_mode((288, 512)) + + red_bird_name = ["redbird-downflap", "redbird-midflap", "redbird-upflap"] + yellow_bird_name = ["yellowbird-downflap", "yellowbird-midflap", "yellowbird-upflap"] blue_bird_name = ["bluebird-downflap", "bluebird-midflap", "bluebird-upflap"] background_name = ["background-day", "background-night"] self.blue_bird = [pygame.image.load(image.join([FILE_ROOT, ".png"])).convert_alpha() for image in blue_bird_name] + self.red_bird = [pygame.image.load(image.join([FILE_ROOT, ".png"])).convert_alpha() + for image in red_bird_name] + self.yellow_bird = [pygame.image.load(image.join([FILE_ROOT, ".png"])).convert_alpha() + for image in yellow_bird_name] self.background = [pygame.image.load(image.join([FILE_ROOT, ".png"])).convert_alpha() for image in background_name] From 56393e11f7615158904a40acc6c98e83a58e5953 Mon Sep 17 00:00:00 2001 From: 01Researcher <13944016111@163.com> Date: Sat, 30 Nov 2024 16:08:52 +0800 Subject: [PATCH 11/15] =?UTF-8?q?11-30,=E6=94=B9=E6=88=90=E4=BA=86?= =?UTF-8?q?=E7=9B=B8=E5=AF=B9=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/entities/bird.py | 72 +++++++++++++++++++++++++++++++++++++++++++- src/utils/manager.py | 2 -- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/src/entities/bird.py b/src/entities/bird.py index 8e761458..41746abc 100644 --- a/src/entities/bird.py +++ b/src/entities/bird.py @@ -1,2 +1,72 @@ -class Bird: +import pygame +import random +import sys + +class Bird(object): def __init__(self): + # 定义一个小鸟类 + self.birdRect = pygame.Rect(65, 50, 50, 50) + self.birdStatus = [[pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\bluebird-downflap.png"),pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\bluebird-midflap.png"),pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\bluebird-upflap.png")], + [pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\redbird-downflap.png"),pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\redbird-midflap.png"),pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\redbird-upflap.png")], + [pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\yellowbird-downflap.png"),pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\yellowbird-midflap.png"),pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\yellowbird-upflap.png")]] + self.status = 0 + self.birdx = 5 + self.birdy = 350 + self.jump = False + self.jumpSpeed = 10 + self.gravity = 5 + self.dead = False + + def birdUpdate(self): # 定义移动方法 + if self.jump: + # 小鸟跳跃状态 + self.jumpSpeed -= 1 + self.birdy = self.birdy - self.jumpSpeed + else: + self.gravity += 0.2 + self.birdy = self.birdy + self.gravity + self.birdRect[1] = self.birdy + +def createMap(): + + colorBird=random.choice(0,3) + if not Bird.dead: + Screen.blit(Bird.birdStatus[colorBird][0], (Bird.birdx, Bird.birdy)) + Bird.birdUpdate() + pygame.display.update() + Screen.blit(Bird.birdStatus[colorBird][1], (Bird.birdx, Bird.birdy)) + Bird.birdUpdate() + pygame.display.update() + Screen.blit(Bird.birdStatus[colorBird][2], (Bird.birdx, Bird.birdy)) + Bird.birdUpdate() + pygame.display.update() + + + pygame.display.update() + for event in pygame.event.get(): + if event.type == pygame.MOUSEBUTTONDOWN and not Bird.dead: + Bird.jump = True + Bird.jumpSpeed = 15 + Bird.gravity = 3 + + if not checkDead(): + createMap() + + +def checkDead(): + #检测小鸟是否死亡 + + upRect=pygame.Rect(Pipeline.wallx,-150,Pipeline.pineUp.get_width(),Pipeline.pineUp.get_height()) + downRect = pygame.Rect(Pipeline.wallx, 400, Pipeline.pineDown.get_width(), Pipeline.pineDown.get_height()) + #检测矩形碰撞 + if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect): + Bird.dead=True + #边界检测 + if not 0 Date: Sat, 7 Dec 2024 15:10:39 +0800 Subject: [PATCH 12/15] 12-7 15:10 --- src/entities/background.py | 17 ++++++++++++++++- src/entities/pipe.py | 22 ++++++++++++++++++++++ src/entities/words.py | 5 ++--- src/game.py | 9 ++++++--- src/utils/manager.py | 26 ++++++++++---------------- src/utils/stats.py | 4 +++- 6 files changed, 59 insertions(+), 24 deletions(-) create mode 100644 src/entities/pipe.py diff --git a/src/entities/background.py b/src/entities/background.py index 60704d99..b1dfa01b 100644 --- a/src/entities/background.py +++ b/src/entities/background.py @@ -1,10 +1,25 @@ 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.image = Manager().background[random.randint(0, 1)] + 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) def draw(self): + #画背景 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 \ No newline at end of file diff --git a/src/entities/pipe.py b/src/entities/pipe.py new file mode 100644 index 00000000..75b2177a --- /dev/null +++ b/src/entities/pipe.py @@ -0,0 +1,22 @@ +import pygame +import random +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.rect = pygame.Rect(100, 300, 52, 320) + + + def draw(self): + self.display_surface.blit(self.image_up, self.rect) + self.display_surface.blit(self.image_down, self.rect.move(0, -(320 + random.randint(80,300)))) + +class Pipes(Pipe): + super().__init__() + diff --git a/src/entities/words.py b/src/entities/words.py index e3fc5fdf..e3bcd431 100644 --- a/src/entities/words.py +++ b/src/entities/words.py @@ -5,11 +5,10 @@ class Words: def __init__(self, display_surface): self.display_surface = display_surface - self.manager = Manager() - self.image_GM = self.manager.gameover + self.image_GM = Manager.get_instance().gameover self.rect_GM = pygame.Rect(48, 235, 192, 42) - self.image_M = self.manager.message + 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) diff --git a/src/game.py b/src/game.py index 338436fb..ea812cfe 100644 --- a/src/game.py +++ b/src/game.py @@ -7,6 +7,7 @@ from src.utils.manager import Manager from src.entities.background import Background from src.entities.words import Words +from src.entities.pipe import Pipe from src.utils.stats import Stats @@ -19,11 +20,12 @@ def __init__(self): self.clock = pygame.time.Clock() self.manager = Manager() self.stats = Stats() + self.stat = self.stats.stat self.background = Background(self.screen) self.words = Words(self.background.image) - + self.pipe = Pipe(self.screen) #self.stats = "gameover" #0=welcome,1=run,2=gameover def handle_events(self): @@ -37,24 +39,25 @@ def handle_events(self): sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: #响应鼠标点击 + print('mouse test') pass def update_screen(self): self.background.draw() - pygame.display.flip() if self.stat == "welcome": self.words.draw_M() pass #画开始界面 elif self.stat == "run": - pass + self.pipe.draw() # 画游戏界面的物品 elif self.stat == "gameover": self.words.draw_GM() pass #画gameover + pygame.display.flip() def main_loop(self): diff --git a/src/utils/manager.py b/src/utils/manager.py index 61d0d564..4bc3b7df 100644 --- a/src/utils/manager.py +++ b/src/utils/manager.py @@ -4,8 +4,6 @@ FILE_ROOT = f"{os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))}/assets/sprites/" class Manager: - # num0 = pygame.image.load(FILE_ROOT + "0.png") - def __init__(self): pygame.init() pygame.display.set_mode((288, 512)) @@ -15,6 +13,7 @@ def __init__(self): yellow_bird_name = ["yellowbird-downflap", "yellowbird-midflap", "yellowbird-upflap"] blue_bird_name = ["bluebird-downflap", "bluebird-midflap", "bluebird-upflap"] background_name = ["background-day", "background-night"] + pipe_name = ["pipe-green", "pipe-red"] self.blue_bird = [pygame.image.load(image.join([FILE_ROOT, ".png"])).convert_alpha() for image in blue_bird_name] @@ -22,23 +21,18 @@ def __init__(self): for image in red_bird_name] self.yellow_bird = [pygame.image.load(image.join([FILE_ROOT, ".png"])).convert_alpha() for image in yellow_bird_name] - + self.numbers = [pygame.image.load(image.join([FILE_ROOT, ".png"])).convert_alpha() + for image in [str(x) for x in range(10)]] self.background = [pygame.image.load(image.join([FILE_ROOT, ".png"])).convert_alpha() for image in background_name] - + self.pipes = [pygame.image.load(image.join([FILE_ROOT, ".png"])).convert_alpha() + for image in pipe_name] + self.base = pygame.image.load(os.path.join(FILE_ROOT, "base.png")).convert_alpha() self.gameover = pygame.image.load(os.path.join(FILE_ROOT, "gameover.png")).convert_alpha() self.message = pygame.image.load(os.path.join(FILE_ROOT, "message.png")).convert_alpha() + @staticmethod + def get_instance(): + return instance - - - - # self.assets_dictionary = {} - # for path, folders, files in os.walk(FILE_ROOT): - # print(files) - # for f in files: - # k = f.split(".")[0] - # self.assets_dictionary[k] = pygame.image.load(os.path.join(path, f)) - # # print(self.assets_dictionary) - -Manager() +instance = Manager() diff --git a/src/utils/stats.py b/src/utils/stats.py index e405a27d..2c8aa88f 100644 --- a/src/utils/stats.py +++ b/src/utils/stats.py @@ -1,4 +1,6 @@ -class Stats: +class Stats: # FIXME: singleton mode implementation stat_list = ["welcome", "run", "gameover"] def __init__(self): + self.stat = self.stat_list[1] + def reset(self): self.stat = self.stat_list[0] From fd33e79cc3220c1055ff8e28c95e09920b81a0b2 Mon Sep 17 00:00:00 2001 From: 01Researcher <13944016111@163.com> Date: Sat, 7 Dec 2024 16:26:49 +0800 Subject: [PATCH 13/15] 12-7 15:10 --- src/entities/bird.py | 84 +++++++++++++++++++++++--------------------- src/entities/pipe.py | 7 ++-- src/game.py | 13 +++++-- src/utils/manager.py | 8 +++-- 4 files changed, 63 insertions(+), 49 deletions(-) diff --git a/src/entities/bird.py b/src/entities/bird.py index 41746abc..a53e4b02 100644 --- a/src/entities/bird.py +++ b/src/entities/bird.py @@ -1,72 +1,74 @@ import pygame import random +from src.utils.manager import Manager import sys class Bird(object): - def __init__(self): + def __init__(self, display_surface): # 定义一个小鸟类 + self.display_surface = display_surface self.birdRect = pygame.Rect(65, 50, 50, 50) - self.birdStatus = [[pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\bluebird-downflap.png"),pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\bluebird-midflap.png"),pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\bluebird-upflap.png")], - [pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\redbird-downflap.png"),pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\redbird-midflap.png"),pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\redbird-upflap.png")], - [pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\yellowbird-downflap.png"),pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\yellowbird-midflap.png"),pygame.image.load(r"C:\Users\dell\PycharmProjects\FlapPyBird\assets\sprites\yellowbird-upflap.png")]] + self.birdStatus = Manager.get_instance().birds + self.clock = pygame.time.Clock() + self.clock.tick(30) self.status = 0 self.birdx = 5 self.birdy = 350 self.jump = False self.jumpSpeed = 10 - self.gravity = 5 + self.gravity = 0.1 self.dead = False + self.colorBird=random.randint(0,2) - def birdUpdate(self): # 定义移动方法 + def birdUpdate(self): + + # 定义移动方法 if self.jump: # 小鸟跳跃状态 self.jumpSpeed -= 1 self.birdy = self.birdy - self.jumpSpeed else: - self.gravity += 0.2 + self.gravity += 0.1 self.birdy = self.birdy + self.gravity self.birdRect[1] = self.birdy -def createMap(): - - colorBird=random.choice(0,3) - if not Bird.dead: - Screen.blit(Bird.birdStatus[colorBird][0], (Bird.birdx, Bird.birdy)) - Bird.birdUpdate() - pygame.display.update() - Screen.blit(Bird.birdStatus[colorBird][1], (Bird.birdx, Bird.birdy)) - Bird.birdUpdate() - pygame.display.update() - Screen.blit(Bird.birdStatus[colorBird][2], (Bird.birdx, Bird.birdy)) - Bird.birdUpdate() - pygame.display.update() + def createMap(self): + if not self.dead: + self.display_surface.blit(self.birdStatus[self.colorBird][0], (self.birdx, self.birdy)) + self.birdUpdate() + self.display_surface.blit(self.birdStatus[self.colorBird][1], (self.birdx, self.birdy)) + self.birdUpdate() + self.display_surface.blit(self.birdStatus[self.colorBird][2], (self.birdx, self.birdy)) + self.birdUpdate() - pygame.display.update() - for event in pygame.event.get(): - if event.type == pygame.MOUSEBUTTONDOWN and not Bird.dead: - Bird.jump = True - Bird.jumpSpeed = 15 - Bird.gravity = 3 - if not checkDead(): - createMap() + for event in pygame.event.get(): + print(event for event in pygame.event.get()) + if event.type == pygame.MOUSEBUTTONDOWN and not self.dead: + self.jump = True + self.jumpSpeed = 15 + self.gravity = 3 + print('1') + # if not checkDead(): + #self.createMap() -def checkDead(): - #检测小鸟是否死亡 - upRect=pygame.Rect(Pipeline.wallx,-150,Pipeline.pineUp.get_width(),Pipeline.pineUp.get_height()) - downRect = pygame.Rect(Pipeline.wallx, 400, Pipeline.pineDown.get_width(), Pipeline.pineDown.get_height()) - #检测矩形碰撞 - if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect): - Bird.dead=True - #边界检测 - if not 0 Date: Sat, 7 Dec 2024 16:32:07 +0800 Subject: [PATCH 14/15] 12-7 16 --- src/game.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/game.py b/src/game.py index 1c102590..11b02869 100644 --- a/src/game.py +++ b/src/game.py @@ -1,5 +1,4 @@ import sys - import pygame #from src.utils.screen import Screen From 95877b2e9dcc84b9d22bc09127369dc4aa26dc6e Mon Sep 17 00:00:00 2001 From: 01Researcher <13944016111@163.com> Date: Sat, 14 Dec 2024 16:41:00 +0800 Subject: [PATCH 15/15] =?UTF-8?q?12-14=20=E5=9F=BA=E6=9C=AC=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/entities/background.py | 1 + src/entities/bird.py | 82 ++++++++++++++++---------------------- src/entities/pipe.py | 39 ++++++++++++++---- src/game.py | 20 ++++++---- src/utils/stats.py | 4 +- 5 files changed, 83 insertions(+), 63 deletions(-) diff --git a/src/entities/background.py b/src/entities/background.py index b1dfa01b..32ae7717 100644 --- a/src/entities/background.py +++ b/src/entities/background.py @@ -12,6 +12,7 @@ def __init__(self, display_surface): self.rect = pygame.Rect(0, 400, 336, 112) def draw(self): + self.stat = Stats().stat #画背景 self.display_surface.blit(self.image, self.image.get_rect()) #画地面 diff --git a/src/entities/bird.py b/src/entities/bird.py index a53e4b02..d1baaaaf 100644 --- a/src/entities/bird.py +++ b/src/entities/bird.py @@ -1,74 +1,60 @@ import pygame import random from src.utils.manager import Manager -import sys +from src.utils.stats import Stats class Bird(object): - def __init__(self, display_surface): + def __init__(self, display_surface, rect): # 定义一个小鸟类 + self.rect_list = rect self.display_surface = display_surface - self.birdRect = pygame.Rect(65, 50, 50, 50) + self.birdRect = pygame.Rect(65, 50, 32, 24) self.birdStatus = Manager.get_instance().birds - self.clock = pygame.time.Clock() - self.clock.tick(30) - self.status = 0 + self.stat = Stats() self.birdx = 5 self.birdy = 350 self.jump = False - self.jumpSpeed = 10 - self.gravity = 0.1 + 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 -= 1 - self.birdy = self.birdy - self.jumpSpeed - else: - self.gravity += 0.1 - self.birdy = self.birdy + self.gravity - self.birdRect[1] = self.birdy + # 小鸟跳跃状态 + 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.display_surface.blit(self.birdStatus[self.colorBird][0], (self.birdx, self.birdy)) - self.birdUpdate() - self.display_surface.blit(self.birdStatus[self.colorBird][1], (self.birdx, self.birdy)) - self.birdUpdate() - self.display_surface.blit(self.birdStatus[self.colorBird][2], (self.birdx, self.birdy)) self.birdUpdate() + def checkDead(self, rect): + #检测小鸟是否死亡 + self.rect_list = rect - for event in pygame.event.get(): - print(event for event in pygame.event.get()) - if event.type == pygame.MOUSEBUTTONDOWN and not self.dead: - self.jump = True - self.jumpSpeed = 15 - self.gravity = 3 - print('1') - - # if not checkDead(): - #self.createMap() - - -# def checkDead(): -# #检测小鸟是否死亡 -# -# upRect=pygame.Rect(Pipeline.wallx,-150,Pipeline.pineUp.get_width(),Pipeline.pineUp.get_height()) -# downRect = pygame.Rect(Pipeline.wallx, 400, Pipeline.pineDown.get_width(), Pipeline.pineDown.get_height()) -# #检测矩形碰撞 -# if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect): -# Bird.dead=True -# #边界检测 -# if not 0