From 4e362001b2316b1524a2b81cc6074ec53662ff74 Mon Sep 17 00:00:00 2001 From: TradingBotsDev Date: Tue, 19 Dec 2023 19:14:49 +0000 Subject: [PATCH] Final Update of the game --- README.md | 40 +++++--- main.py | 282 ++++++++++++++++++++++-------------------------------- 2 files changed, 139 insertions(+), 183 deletions(-) diff --git a/README.md b/README.md index 078a3ed..8bf67bc 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,46 @@ -# https://github.com/LeonMarqs/Flappy-bird-python/blob/master/flappy.py +#Offical FlappyBird Game -# The Use of FlappyBird for Reinforcement Learning +To run this Project Follow the Steps Below -# The Purpose of this Project is to increase My AI skills -The First Part of the Development is to develop the Game +#Git Clone The repo -Then I will use Reinforcement to Play the Game +[git clone https://github.com/DevToolX/FlappyBirdGame.git] -There is some Fix, that the Game don't keep Track of the High score -There is no dificulty involve in the game +#Install the Requirements for the +[pip install -r requirements. txt ] -# AI +or +[pip3 install -r requirements. txt] -The AI will be develop so that it have no acess to the codebase +#Then run main.py File -Only the the output of the code of the game playing the code will be able to see +This will result in the game and you able to play. -Then the code will teach it self to play the Game +MIT License -The techStack I will be using is the OpenCV to see the screen play and -Then giving the code access to the button and then Learn how to play +Copyright (c) 2019 Leonardo Marques +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/main.py b/main.py index 797bd6e..7b186a4 100644 --- a/main.py +++ b/main.py @@ -1,15 +1,10 @@ -import time, random, pygame +import pygame, random, time from pygame.locals import * - - - - -#Declare the Global Variables - +#VARIABLES SCREEN_WIDHT = 400 SCREEN_HEIGHT = 600 -SPEED = 20 +SPEED = 20 GRAVITY = 2.5 GAME_SPEED = 15 @@ -18,150 +13,26 @@ PIPE_WIDHT = 80 PIPE_HEIGHT = 500 -PIPE_GAP = 150 - - - - -"""" -This Class I will use to control the Game and what comes before and after -""" - -class MasterClass(pygame.sprite.Sprite): - - def __init__(self): - pygame.init() - self.screen = pygame.display.set_mode((SCREEN_WIDHT, SCREEN_HEIGHT)) - pygame.display.set_caption('Flappy Bird') - - self.BACKGROUND = pygame.image.load('assets/sprites/background-day.png') - self.BACKGROUND = pygame.transform.scale(self.BACKGROUND, (SCREEN_WIDHT, SCREEN_HEIGHT)) - self.BEGIN_IMAGE = pygame.image.load('assets/sprites/message.png').convert_alpha() - - - #Define the Sounds that the birds makes - self.wing = 'assets/audio/wing.wav' - self.hit = 'assets/audio/hit.wav' - - self.bird_group = pygame.sprite.Group() - self.bird = Bird() - self.bird_group.add(self.bird) - - self.ground_group = pygame.sprite.Group() - - for i in range (2): - self.ground = Ground(GROUND_WIDHT * i) - self.ground_group.add(self.ground) - - - self.pipe_group = pygame.sprite.Group() - for i in range (2): - self.pipes = gamePlay.get_random_pipes(SCREEN_WIDHT * i + 800) - self.pipe_group.add(self.pipes[0]) - self.pipe_group.add(self.pipes[1]) - - - - self.clock = pygame.time.Clock() - self.begin = True - - while self.begin: - - self.clock.tick(15) - - for event in pygame.event.get(): - if event.type == QUIT: - pygame.quit() - - if event.type == KEYDOWN: - if event.key == K_SPACE or event.key == K_UP: - self.bird.bump() - pygame.mixer.music.load(self.wing) - pygame.mixer.music.play() - self.begin = False - - - self.screen.blit(self.BACKGROUND, (0, 0)) - self.screen.blit(self.BEGIN_IMAGE, (120, 150)) - - if gamePlay.is_off_screen(self.ground_group.sprites()[0]): - self.ground_group.remove(self.ground_group.sprites()[0]) - - self.new_ground = Ground(GROUND_WIDHT - 20) - self.ground_group.add(self.new_ground) - - self.bird.begin() - self.ground_group.update() - - self.bird_group.draw(self.screen) - self.ground_group.draw(self.screen) - - pygame.display.update() - - - while True: - - self.clock.tick(15) - - for event in pygame.event.get(): - if event.type == QUIT: - pygame.quit() - if event.type == KEYDOWN: - if event.key == K_SPACE or event.key == K_UP: - self.bird.bump() - pygame.mixer.music.load(self.wing) - pygame.mixer.music.play() - - self.screen.blit(self.BACKGROUND, (0, 0)) - if gamePlay.is_off_screen(self.ground_group.sprites()[0]): - self.ground_group.remove(self.ground_group.sprites()[0]) - - self.new_ground = Ground(GROUND_WIDHT - 20) - self.ground_group.add(self.new_ground) - - if gamePlay.is_off_screen(self.pipe_group.sprites()[0]): - self.pipe_group.remove(self.pipe_group.sprites()[0]) - self.pipe_group.remove(self.pipe_group.sprites()[0]) - - pipes = gamePlay.get_random_pipes(SCREEN_WIDHT * 2) - - self.pipe_group.add(pipes[0]) - self.pipe_group.add(pipes[1]) - - self.bird_group.update() - self.ground_group.update() - self.pipe_group.update() - - self.bird_group.draw(self.screen) - self.pipe_group.draw(self.screen) - self.ground_group.draw(self.screen) - - pygame.display.update() - - if (pygame.sprite.groupcollide(self.bird_group, self.ground_group, False, False, pygame.sprite.collide_mask) or - pygame.sprite.groupcollide(self.bird_group, self.pipe_group, False, False, pygame.sprite.collide_mask)): - pygame.mixer.music.load(self.hit) - pygame.mixer.music.play() - time.sleep(1) - +PIPE_GAP = 150 +wing = 'assets/audio/wing.wav' +hit = 'assets/audio/hit.wav' +pygame.mixer.init() class Bird(pygame.sprite.Sprite): - def __init__(self) -> None: + def __init__(self): pygame.sprite.Sprite.__init__(self) - self.images = [pygame.image.load('assets/sprites/bluebird-upflap.png').convert_alpha(), pygame.image.load('assets/sprites/bluebird-midflap.png').convert_alpha(), pygame.image.load('assets/sprites/bluebird-downflap.png').convert_alpha()] - self.speed= SPEED + self.speed = SPEED - self.current_image = 0 self.image = pygame.image.load('assets/sprites/bluebird-upflap.png').convert_alpha() self.mask = pygame.mask.from_surface(self.image) @@ -170,8 +41,6 @@ def __init__(self) -> None: self.rect[0] = SCREEN_WIDHT / 6 self.rect[1] = SCREEN_HEIGHT / 2 - - def update(self): self.current_image = (self.current_image + 1) % 3 self.image = self.images[self.current_image] @@ -180,23 +49,15 @@ def update(self): #UPDATE HEIGHT self.rect[1] += self.speed - def bump(self): self.speed = -SPEED - def begin(self): self.current_image = (self.current_image + 1) % 3 self.image = self.images[self.current_image] -""" - This Part of the Code deals with the Pipes of the Game - -""" - - class Pipe(pygame.sprite.Sprite): @@ -223,9 +84,7 @@ def __init__(self, inverted, xpos, ysize): def update(self): self.rect[0] -= GAME_SPEED -""" -The Management of the Ground of the Game -""" + class Ground(pygame.sprite.Sprite): @@ -242,38 +101,121 @@ def __init__(self, xpos): def update(self): self.rect[0] -= GAME_SPEED +def is_off_screen(sprite): + return sprite.rect[0] < -(sprite.rect[2]) + +def get_random_pipes(xpos): + size = random.randint(100, 300) + pipe = Pipe(False, xpos, size) + pipe_inverted = Pipe(True, xpos, SCREEN_HEIGHT - size - PIPE_GAP) + return pipe, pipe_inverted + + +pygame.init() +screen = pygame.display.set_mode((SCREEN_WIDHT, SCREEN_HEIGHT)) +pygame.display.set_caption('Flappy Bird') + +BACKGROUND = pygame.image.load('assets/sprites/background-day.png') +BACKGROUND = pygame.transform.scale(BACKGROUND, (SCREEN_WIDHT, SCREEN_HEIGHT)) +BEGIN_IMAGE = pygame.image.load('assets/sprites/message.png').convert_alpha() + +bird_group = pygame.sprite.Group() +bird = Bird() +bird_group.add(bird) + +ground_group = pygame.sprite.Group() + +for i in range (2): + ground = Ground(GROUND_WIDHT * i) + ground_group.add(ground) + +pipe_group = pygame.sprite.Group() +for i in range (2): + pipes = get_random_pipes(SCREEN_WIDHT * i + 800) + pipe_group.add(pipes[0]) + pipe_group.add(pipes[1]) + + + +clock = pygame.time.Clock() + +begin = True + +while begin: + + clock.tick(15) + + for event in pygame.event.get(): + if event.type == QUIT: + pygame.quit() + if event.type == KEYDOWN: + if event.key == K_SPACE or event.key == K_UP: + bird.bump() + pygame.mixer.music.load(wing) + pygame.mixer.music.play() + begin = False + + screen.blit(BACKGROUND, (0, 0)) + screen.blit(BEGIN_IMAGE, (120, 150)) + + if is_off_screen(ground_group.sprites()[0]): + ground_group.remove(ground_group.sprites()[0]) + + new_ground = Ground(GROUND_WIDHT - 20) + ground_group.add(new_ground) + + bird.begin() + ground_group.update() + bird_group.draw(screen) + ground_group.draw(screen) + pygame.display.update() -class gamePlay: +while True: + clock.tick(15) - def is_off_screen(sprite): - return sprite.rect[0] < -(sprite.rect[2]) + for event in pygame.event.get(): + if event.type == QUIT: + pygame.quit() + if event.type == KEYDOWN: + if event.key == K_SPACE or event.key == K_UP: + bird.bump() + pygame.mixer.music.load(wing) + pygame.mixer.music.play() + screen.blit(BACKGROUND, (0, 0)) - #Also need to increase the dificullty of the game base on the score - #I will Add an if Statment to check the score and change the Pipes Hight - def get_random_pipes(xpos): - size = random.randint(100, 300) - pipe = Pipe(False, xpos, size) - pipe_inverted = Pipe(True, xpos, SCREEN_HEIGHT - size - PIPE_GAP) - return pipe, pipe_inverted + if is_off_screen(ground_group.sprites()[0]): + ground_group.remove(ground_group.sprites()[0]) + new_ground = Ground(GROUND_WIDHT - 20) + ground_group.add(new_ground) + if is_off_screen(pipe_group.sprites()[0]): + pipe_group.remove(pipe_group.sprites()[0]) + pipe_group.remove(pipe_group.sprites()[0]) - #Keep Tracks of the High score of the Game - #Use a context Manager - def highScore(): - #Define the Path - with open("highscore.json", "r+") as score: - score.dump() + pipes = get_random_pipes(SCREEN_WIDHT * 2) - pass + pipe_group.add(pipes[0]) + pipe_group.add(pipes[1]) + bird_group.update() + ground_group.update() + pipe_group.update() + bird_group.draw(screen) + pipe_group.draw(screen) + ground_group.draw(screen) + pygame.display.update() -if __name__ == "__main__": - MasterClass() \ No newline at end of file + if (pygame.sprite.groupcollide(bird_group, ground_group, False, False, pygame.sprite.collide_mask) or + pygame.sprite.groupcollide(bird_group, pipe_group, False, False, pygame.sprite.collide_mask)): + pygame.mixer.music.load(hit) + pygame.mixer.music.play() + time.sleep(1) + break