Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
6.3 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_bullet = "bull1.png"
img_hero = "shatl3.png"
img_enemy = "ufo2.png"
img_boss = "ufo2.png"
img_bullet2 = "bul2.png"
score = 0
goal = 5
lost = 0
max_lost = 3
counter = 0

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 = Null

    def ChangeState(self, new_state):
        self.current_state.Exit()
        new_state.Enter()


class Player(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.reload_time = 0
        self.cooldown = 10
    def update(self):
        self.reload_time -= 1

        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 <= 0:
            self.reload_time = self.cooldown
            bullet = Bullet(img_bullet, self.rect.centerx, self.rect.top, 15, 20, -15)
            bullets.add(bullet)
class Enemy(GameSprite):
    def update(self):
        self.rect.y += self.speed
        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):
        super().__init__( player_image, player_x, player_y, size_x, size_y, player_speed)
        self.reload_time = 0
        self.cooldown = 6
    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))
background = transform.scale(image.load(img_back), (win_width, win_height))
ship = Player(img_hero, 5, win_height - 150,      80, 100     , 10)
ship.hp = 15
monsters = sprite.Group()
for i in range(1, 6):
    monster = Enemy(img_enemy, randint(80, win_width - 80), -40, 80, 50, randint(1, 5))
    monsters.add(monster)
bullets = sprite.Group()
bullets2 = sprite.Group()
players = sprite.Group()
players.add(ship)
finish = False
run = True
boss_fight = False
while run:
    for e in event.get():
        if e.type == QUIT:
            run = False
        elif e.type == MOUSEBUTTONDOWN:
            if e.button == 1:
                ship.fire()
    if not finish:
        window.blit(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))
        ship.update()
        monsters.update()
        bullets.update()
        ship.reset()
        monsters.draw(window)
        bullets.draw(window)
        bullets2.update()
        bullets2.draw(window)
        if not boss_fight:
            collides = sprite.groupcollide(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))
                monsters.add(monster)
        else:
            collides = sprite.groupcollide(monsters, bullets, False, True)
            for c in collides:
                score = score + 1
        collides = sprite.groupcollide(players, bullets2, False, True)
        for c in collides:
            score = score - 1

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

        if score >= goal:
            if not boss_fight:
                boss_fight = True
                goal += 20
                monsters.empty()
                boss = Boss(img_boss, randint(80, win_width - 80) ,0, 160, 100,5)
                monsters.add(boss)
                boss.fir()
               
            else:
                finish = True
                window.blit(win, (200, 200))
        display.update()
    time.delay(50)
Leave a Comment