Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
10 kB
1
Indexable
Never
from pygame import *
from random import randint
font.init()
font1 = font.Font(None, 80)
win = font1.render('YOU WIN!', True, (255, 255, 255))
lose = font1.render('YOU LOSE!', True, (180, 0, 0))
font2 = font.Font(None, 36)
img_back = "galaxy.jpg"
img_back2 = "cor.jpg"
img_bullet = "bullet.png"
img_hero = "shatl3.png"
img_enemy = "ufo2.png"
img_boss = "ufo2.png"
img_bullet2 = "bullet.png"
score = 0
goal = 5
lost = 0
max_lost = 3
counter = 0
cooldown_bonus = 0
hp_bonus = 0
money = 10000


class GameSprite(sprite.Sprite):
    def __init__(self, player_image, player_x, player_y, size_x, size_y, player_speed):
        sprite.Sprite.__init__(self)
        self.image = transform.scale(image.load(player_image), (size_x, size_y))
        self.speed = player_speed
        self.rect = self.image.get_rect()
        self.rect.x = player_x
        self.rect.y = player_y

    def reset(self):
        window.blit(self.image, (self.rect.x, self.rect.y))


class State():
    def Enter(self):
        pass
        # Показать игрока, заспавнить мобов

    def Update(self):
        pass
        # Обработать столкновения, посчитать очки, передвинуть спрайты

    def Exit(self):
        pass
        # Удалить все спрайты, пули мобов, боссов и так далее


class StateMachine():
    def __init__(self):
        #self.current_state = #must have
        self.run = True

    def Change_State(self, new_state):
        self.current_state.Exit()
        new_state.Enter()
        self.current_state = new_state

    def Update(self):
        self.events = event.get()
        #Выход из игры
        for e in self.events:
            if e.type == QUIT:
                self.run = False

        self.current_state.Update()


class Level():
    def __init__(self,monster_list,boss):
        self.monster_list = monster_list
        self.boss = boss





class Player(GameSprite):
    def __init__(self, player_image, player_x, player_y, size_x, size_y, player_speed,hp):
        super().__init__(player_image, player_x, player_y, size_x, size_y, player_speed)
        global cooldown_bonus,hp_bonus
        self.reload_time = 0
        self.cooldown = 10 + cooldown_bonus * 5
        self.hp = hp + hp_bonus
    def update(self):
        self.reload_time += self.cooldown

        keys = key.get_pressed()
        if keys[K_a] and self.rect.x > 5:
            self.rect.x -= self.speed
        if keys[K_d] and self.rect.x < win_width - 80:
            self.rect.x += self.speed

    def fire(self):
        if self.reload_time >= 100:
            self.reload_time = 0
            bullet = Bullet(img_bullet, self.rect.centerx, self.rect.top, 15, 20, -15)
            bullets.add(bullet)
    def take_damage(self,damage):
        global finish
        self.hp -= damage
        if self.hp <= 0:
            finish = True
            window.blit(lose, (200, 200))




class Enemy(GameSprite):
    def update(self):
        self.rect.y += self.speed
        self.damage = 1
        global lost
        if self.rect.y > win_height:
            self.rect.x = randint(80, win_width - 80)
            self.rect.y = 0
            lost = lost + 1


class Boss(GameSprite):
    def __init__(self, player_image, player_x, player_y, size_x, size_y, player_speed,damage):
        super().__init__(player_image, player_x, player_y, size_x, size_y, player_speed)
        self.reload_time = 0
        self.cooldown = 6
        self.damage = damage


    def update(self):
        self.reload_time -= 1
        if self.reload_time <= 0:
            self.reload_time = self.cooldown
            self.fir()

        self.rect.x += self.speed
        if self.rect.x > win_width - 80:
            self.speed *= -1
            self.speed -= 1

        elif self.rect.x < -80:
            self.speed *= -1
            self.speed += 1

    def fir(self):
        bullet2 = Bullet(img_bullet2, self.rect.centerx, self.rect.top, 15, 20, 15)
        bullet2.x_speed = randint(-2, 2) * 5 + self.speed
        bullets2.add(bullet2)


class Bullet(GameSprite):
    def __init__(self, player_image, player_x, player_y, size_x, size_y, player_speed):
        super().__init__(player_image, player_x, player_y, size_x, size_y, player_speed)
        self.x_speed = 0

    def update(self):
        self.rect.y += self.speed
        self.rect.x += self.x_speed
        if self.rect.y < 0 or self.rect.y > win_height:
            self.kill()


win_width = 700
win_height = 500
display.set_caption("Shooter")
window = display.set_mode((win_width, win_height))

class GameState(State):
    def __init__(self, machine):
        self.machine = machine

    def Enter(self):
        global bullets, bullets2,money,score
        self.background = transform.scale(image.load(img_back), (win_width, win_height))
        self.ship = Player(img_hero, 5, win_height - 150, 80, 100, 10, 10)

        self.monsters = sprite.Group()
        for i in range(1, 6):
            monster = Enemy(img_enemy, randint(80, win_width - 80), -40, 80, 50, randint(1, 5))
            self.monsters.add(monster)
        bullets = sprite.Group()
        bullets2 = sprite.Group()
        self.players = sprite.Group()
        self.players.add(self.ship)
        self.finish = False
        self.boss_fight = False
        money += score
        score = 0

    def Update(self):
        global  window, score, lost, max_lost, goal, display, bullets
        for e in self.machine.events:
            if e.type == MOUSEBUTTONDOWN:
                if e.button == 1:
                    self.ship.fire()
        if not self.finish:
            window.blit(self.background, (0, 0))
            text = font2.render("Счет: " + str(score), 1, (255, 255, 255))
            window.blit(text, (10, 20))
            text_lose = font2.render("Пропущено: " + str(lost), 1, (255, 255, 255))
            window.blit(text_lose, (10, 50))
            self.ship.update()
            self.monsters.update()
            bullets.update()
            self.ship.reset()
            self.monsters.draw(window)
            bullets.draw(window)
            bullets2.update()
            bullets2.draw(window)
            if not self.boss_fight:
                collides = sprite.groupcollide(self.monsters, bullets, True, True)
                for c in collides:
                    score = score + 1
                    monster = Enemy(img_enemy, randint(80, win_width - 80), -40, 80, 50, randint(1, 5))
                    self.monsters.add(monster)
            else:
                collides = sprite.groupcollide(self.monsters, bullets, False, True)
                for c in collides:
                    score = score + 1
            collides = sprite.groupcollide(self.players, bullets2, False, True)
            for c in collides:
                #score = score - 1
                self.ship.take_damage(1)

            #if sprite.spritecollide(self.ship, self.monsters, False) or lost >= max_lost or score <= -1:
            #    self.finish = True
            #    window.blit(lose, (200, 200))

            if sprite.spritecollide(self.ship, self.monsters, False):
                self.ship.take_damage(2) #Урон при столкновении с монстром

            if lost >= max_lost or self.ship.hp <= 0: #Проигрыш при 0 ХП
                self.finish = True
                window.blit(lose, (200, 200))

            if score >= goal:
                if not self.boss_fight:
                    self.boss_fight = True
                    goal += 7
                    self.monsters.empty()
                    self.boss = Boss(img_boss, randint(80, win_width - 80), 0, 160, 100, 5,1)
                    self.monsters.add(self.boss)
                    self.boss.fir()

                else:
                    self.finish = True
                    window.blit(win, (200, 200))
            display.update()

        else:
            for e in self.machine.events:
                if e.type == KEYDOWN and e.key == K_SPACE:
                    self.machine.Change_State(self.menu_state)

        time.delay(50)

class MenuState(State):
    def __init__(self, machine, game_state):
        self.machine = machine
        self.game_state = game_state



    def Enter(self):
        self.background = transform.scale(image.load(img_back2), (win_width, win_height))

    def Update(self):
        global  window, display,money,hp_bonus,cooldown_bonus

        for e in self.machine.events:
            if e.type == KEYDOWN and e.key == K_SPACE:
                self.machine.Change_State(self.game_state)
            elif e.type == KEYDOWN and e.key == K_q:
                if money >= 10 + hp_bonus * 5:
                    hp_bonus += 1
                    money -= 10 + hp_bonus * 5
                    print("work hp")
                else:
                    pass#проиграть звук неудачи

            elif e.type == KEYDOWN and e.key == K_w:
               if money >= 10 + cooldown_bonus * 5:
                   cooldown_bonus += 1
                   money -= 10 + cooldown_bonus * 5
                   print("work cooldown")
               else:
                   pass  # проиграть звук неудачи


        window.blit(self.background, (0, 0))
        text = font2.render("Меню", 1, (255, 255, 255))
        window.blit(text, (10, 20))

        text_lose = font2.render("Нажмите пробел чтобы играть", 1, (255, 255, 255))
        window.blit(text_lose, (10, 50))
        text_lose = font2.render("Нажмите Z чтобы играть", 1, (255, 255, 255))
        window.blit(text_lose, (10, 50))


        display.update()
        time.delay(50)


state_machine = StateMachine()

game_state = GameState(state_machine)
menu_state = MenuState(state_machine, game_state)

game_state.menu_state = menu_state

state_machine.current_state = menu_state
state_machine.current_state.Enter()

while state_machine.run:
    state_machine.Update()
Leave a Comment