Создание задержки для победы или потери изображения - PullRequest
0 голосов
/ 28 декабря 2018

В своем коде я пытаюсь создать игру, и когда вы наберете определенное количество очков, вы получите либо выигрышный, либо проигрышный знак.но проблема, с которой я сталкиваюсь, заключается в том, что экран проигрыша или выигрыша не отображается в течение достаточного времени.

Если я попытаюсь использовать такую ​​функцию, как pygame.time.wait () или pygame.time.wait (), либоиз них просто остановите игру на кадре, где последняя пуля либо заставляет вас выиграть, либо, к сожалению, показывает только то, что вы проиграли, или то, что вы на экране, в течение примерно одной сотой секунды, а это не то, что я хочу.

import pygame
import sys
import pygame.freetype
import random
import time


pygame.init()
pygame.font.init()
pygame.init()
pygame.mixer.init()
pygame.display.set_caption("this game")
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()  # A clock to limit the frame rate.

bomb =pygame.font.get_fonts()

#print(bomb)

class Background:
    picture = pygame.image.load("C:/images/space.jpg").convert()
    picture = pygame.transform.scale(picture, (1280, 720))

    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))



class Loosing:
    picture = pygame.image.load("C:/images/eye.jpg").convert()
    picture = pygame.transform.scale(picture, (1280, 720))

    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))


class Winning:
    picture = pygame.image.load("C:/images/lightspeed.jpeg").convert()
    picture = pygame.transform.scale(picture, (1280, 720))

    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))



class Annoy:
    picture = pygame.image.load("C:/aliens/Ocram_animated_2.gif").convert()
    picture = pygame.transform.scale(picture, (200, 200))
    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y
        self.speed_x = 0
        self.speed_y = 0
        self.rect = self.picture.get_rect()


    def update(self):
        self.xpos += self.speed_x
        self.ypos += self.speed_y


    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))




class Player_one:
    picture = pygame.image.load("C:/aliens/ezgif.com-crop.gif")
    picture = pygame.transform.scale(picture, (200, 200))

    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y
        self.speed_x = 0
        self.speed_y = 0
        self.rect = self.picture.get_rect()
        self.rect.x = self.xpos
        self.rect.y = self.ypos

    def update(self):
        self.xpos += self.speed_x
        self.ypos += self.speed_y
        self.rect.x = self.xpos
        self.rect.y = self.ypos

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))



class Player_two:
    picture = pygame.image.load("C:/aliens/Giantmechanicalcrab2 - Copy.gif")
    picture = pygame.transform.scale(picture, (300, 200))

    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y
        self.speed_x = 0
        self.speed_y = 0
        self.rect = self.picture.get_rect()
        self.rect.x = self.xpos
        self.rect.y = self.ypos

    def update(self):
        self.xpos += self.speed_x
        self.ypos += self.speed_y
        self.rect.x = self.xpos
        self.rect.y = self.ypos

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))



class Bullet_player_one(pygame.sprite.Sprite):
    picture = pygame.image.load("C:/aliens/giphy.gif").convert_alpha()
    picture = pygame.transform.scale(picture, (100, 100))
    def __init__(self, owner, start_x, start_y, speed_x):
        self.owner = owner
        self.xpos = start_x
        self.ypos = start_y
        self.speed_x = speed_x
        super().__init__()
        self.rect = self.picture.get_rect()
        self.rect.x = self.xpos
        self.rect.y = self.ypos

    def update(self):
        self.xpos += self.speed_x
        self.rect.x = self.xpos
        self.rect.y = self.ypos


        if self.rect.right < 0 or self.rect.left > screen.get_width():
            self.kill()

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))

    def is_collided_with(self, sprite):
        return self.rect.colliderect(sprite.rect)


class Bullet_player_two(pygame.sprite.Sprite):
    picture = pygame.image.load("C:/aliens/MajesticLavishBackswimmer-size_restricted.gif").convert_alpha()
    picture = pygame.transform.scale(picture, (100, 100))
    picture = pygame.transform.rotate(picture, 180)
    def __init__(self, owner, start_x, start_y, speed_x):
        self.owner = owner
        self.xpos = start_x
        self.ypos = start_y
        self.speed_x = speed_x
        super().__init__()
        self.rect = self.picture.get_rect()
        self.rect.x = self.xpos
        self.rect.y = self.ypos

    def update(self):
        self.xpos += self.speed_x
        self.rect.x = self.xpos
        self.rect.y = self.ypos
        if self.rect.right < 0 or self.rect.left > screen.get_width():
            self.kill()

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))

    def is_collided_with(self, sprite):
        return self.rect.colliderect(sprite.rect)







class Bullet_right(pygame.sprite.Sprite):
    picture = pygame.image.load("C:/aliens/big_boy.png").convert_alpha()
    picture = pygame.transform.scale(picture, (100, 100))
    def __init__(self, owner, start_x, start_y, speed_x):
        self.owner = owner
        self.xpos = start_x
        self.ypos = start_y
        self.speed_x = speed_x
        super().__init__()
        self.rect = self.picture.get_rect()
        self.rect.x = self.xpos
        self.rect.y = self.ypos

    def update(self):
        self.xpos += self.speed_x
        self.rect.x = self.xpos
        self.rect.y = self.ypos
        if self.rect.right < 0 or self.rect.left > screen.get_width():
            self.kill()

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))

    def is_collided_with(self, sprite):
        return self.rect.colliderect(sprite.rect)









class Bullet_left(pygame.sprite.Sprite):
    picture = pygame.image.load("C:/aliens/blinking_doge_by_euamodeus-d7vjq7m.gif").convert_alpha()
    picture = pygame.transform.scale(picture, (100, 100))
    def __init__(self, owner, start_x, start_y, speed_x):
        self.owner = owner
        self.xpos = start_x
        self.ypos = start_y
        self.speed_x = speed_x
        super().__init__()
        self.rect = self.picture.get_rect()
        self.rect.x = self.xpos
        self.rect.y = self.ypos

    def update(self):
        self.xpos += self.speed_x
        self.rect.x = self.xpos
        self.rect.y = self.ypos
        if self.rect.right < 0 or self.rect.left > screen.get_width():
            self.kill()

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))

    def is_collided_with(self, sprite):
        return self.rect.colliderect(sprite.rect)


#class Text:
#    def __init__(self, x, y, texty, score):
#        self.ypos = y
#        self.xpos = x
#        self.text = texty
#        self.score = score
#
#    def update(self):
#        
#
#    def draw(self):
#        screen.blit(self.picture, (self.xpos, self.ypos))




player_one = Player_one(0, 500)
player_two = Player_two(1000, 0)
skull = Annoy(520, -100)
cliff = Background(0, 0)
player_one_bullet_list = pygame.sprite.Group()
player_two_bullet_list = pygame.sprite.Group()
right_list = pygame.sprite.Group()
left_list = pygame.sprite.Group()
player_one_bullet = None
player_two_bullet = None
left = None
right = None
count = 100
wait = 100
locked = True
lockered = True
player_one_score = 0
player_two_score = 0
loaded = True
ready = True
lose = Loosing(0, 0)
win = Winning(0, 0)
word = ""
difficulty = input("easy, medium or hard")
locked = False
ping = False
done = False
diff = []
on_ground = False
if difficulty != "easy" and difficulty != "medium" and difficulty != "hard":
    while difficulty != "easy" and difficulty != "medium" and difficulty != "hard":
        difficulty = input("easy, medium or hard")

while True:


    basicfont = pygame.font.SysFont('impact', 48)
    text = basicfont.render(' player one Score:' + str(player_one_score), True, (255, 125, 0), (0, 0, 0))
    textrect = text.get_rect()
    textrect.centerx = screen.get_rect().centerx
    textrect.centery = screen.get_rect().centery



    basicfont_two = pygame.font.SysFont('impact', 48)
    text_two = basicfont.render('player two Score:' + str(player_two_score) , True, (255, 0, 0), (0, 0, 0))
    textrect = text.get_rect()
    textrect.centerx = screen.get_rect().centerx
    textrect.centery = screen.get_rect().centery




    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_w:
                player_one.speed_y = -5

            elif event.key == pygame.K_s:
                player_one.speed_y = 5

            #elif event.key == pygame.K_UP:
                #player_two.speed_y = -5

            #elif event.key == pygame.K_DOWN:
                #player_two.speed_y = 5

            elif event.key == pygame.K_SPACE:
                if len(player_one_bullet_list) == 0:
                    player_one_bullet = Bullet_player_one(player_one, player_one.xpos, player_one.ypos, 20)
                    player_one_bullet_list.add(player_one_bullet)
                    pygame.mixer.Channel(0).play(pygame.mixer.Sound("C:/sounds/Big_Explosion_Cut_Off.wav"))






            #elif event.key == pygame.K_KP0:










        elif event.type == pygame.KEYUP:
            # Stop moving when the keys are released.
            if event.key == pygame.K_s and player_one.speed_y > 0:
                player_one.speed_y = 0
            elif event.key == pygame.K_w and player_one.speed_y < 0:
                player_one.speed_y = 0

            if event.key == pygame.K_DOWN and player_two.speed_y > 0:
                player_two.speed_y = 0
            elif event.key == pygame.K_UP and player_two.speed_y < 0:
                player_two.speed_y = 0


    if difficulty == "easy":
        choice = random.randint(1, 10)
        choosen = random.randint(1, 10)
        if choice == 2 and choosen == 4 or choice == 4 and choosen == 2:
            if len(player_two_bullet_list) == 0:
                player_two_bullet = Bullet_player_two(player_two, player_two.xpos, player_two.ypos, -20)
                player_two_bullet_list.add(player_two_bullet)
                pygame.mixer.Channel(1).play(pygame.mixer.Sound("C:/sounds/Dumpster_Rattle.wav"))

    elif difficulty == "medium":
        choice = random.randint(1, 10)
        if choice == 2 or choice == 4 or choice == 6 or choice == 8 or choice == 10:
            if len(player_two_bullet_list) == 0:
                player_two_bullet = Bullet_player_two(player_two, player_two.xpos, player_two.ypos, -20)
                player_two_bullet_list.add(player_two_bullet)
                pygame.mixer.Channel(1).play(pygame.mixer.Sound("C:/sounds/Dumpster_Rattle.wav"))


    else:
        choice = random.randint(1, 10)
        if choice == 2 or choice == 4 or choice == 6 or choice == 8 or choice == 10 or choice == 5 or choice == 7 or choice == 9 or choice == 1:
            if len(player_two_bullet_list) == 0:
                player_two_bullet = Bullet_player_two(player_two, player_two.xpos, player_two.ypos, -20)
                player_two_bullet_list.add(player_two_bullet)
                pygame.mixer.Channel(1).play(pygame.mixer.Sound("C:/sounds/dumper.wav"))





    if difficulty == "easy":
        chooso = random.randint(1, 10)
        if chooso == 9 or chooso == 4:
            if on_ground == False:
                choosing = random.randint(1, 10)
                if choosing == 3:
                    skull.speed_y = 10
                    if skull.ypos == 520:
                            on_ground = True
            else:
                skull.speed_y = -10
                if skull.ypos == -100:
                    on_ground = False

        else:
            skull.speed_y = 0

        choosa = random.randint(1, 10)
        locking = random.randint(1, 10)
        if choosa == 3 and locking == 7 and len(right_list) == 0:
            right = Bullet_right(skull, skull.xpos, skull.ypos, -20) #should be left
            right_list.add(right)

        elif choosa == 4 and locking == 8 and len(left_list) == 0:
            left = Bullet_left(skull, skull.xpos, skull.ypos, 20) #should be right
            left_list.add(left)




    if player_one.ypos == 520:
        player_one.speed_y = -5

    if player_one.ypos == 0:
        player_one.speed_y = +5

    if player_two.ypos == 520:
        player_two.speed_y = -5

    if player_two.ypos == 0:
        player_two.speed_y = +5


#    if player_one_bullet.xpos == 520:
#        player_one_bullet.kill()

    player_one.update()
    player_two.update()
    skull.update()
    cliff.draw()
    player_one.draw()
    player_two.draw()
    skull.draw()
    screen.blit(text, (0, 0))
    screen.blit(text_two, (700, 0))

    player_one_bullet_list.update()
    player_two_bullet_list.update()
    left_list.update()
    right_list.update()

    for player_one_bullet in player_one_bullet_list:
        player_one_bullet.draw()

    for player_two_bullet in player_two_bullet_list:
        player_two_bullet.draw()

    for left in left_list:
        left.draw()

    for right in right_list:
        right.draw()

    for bullet in player_one_bullet_list:
        if bullet.is_collided_with(player_two):
            player_one_bullet.kill()
            player_one_score +=1
            pygame.mixer.Channel(2).play(pygame.mixer.Sound("C:/sounds/Beep_Short.wav"))


            #pygame.mixer.music.load("C:/sounds/hammond.wav")
            #pygame.mixer.music.play(3)


    for bullet in player_two_bullet_list:
        if bullet.is_collided_with(player_one):
            player_two_bullet.kill()
            player_two_score +=1
            pygame.mixer.Channel(2).play(pygame.mixer.Sound("C:/sounds/Emergency_Siren_Short_Burst.wav"))

#        if bullet.is_collided_with(skull):
#            player_two_bullet.kill()
#            if player_two_score == 0:
#                player_two_score = 0
#            elif player_two_score > 0:
#                player_two_score = player_two_score-1
#        elif bullet.is_collided_with(player_one_bullet):
#            player_one_bullet.kill()
#            player_two_bullet.kill()
            #pygame.mixer.music.load("C:/sounds/clarkson.wav")
            #pygame.mixer.music.play(3)


    for right in right_list:
        if right.is_collided_with(player_one):
            right.kill()
            if player_one_score == 0:
                player_one_score = 0
            else:
                player_one_score += -1

    for left in left_list:
        if left.is_collided_with(player_two):
            left.kill()
            if player_two_score == 0:
                player_two_score = 0
            else:
                player_two_score += -1




    if player_one_score == 100:
        win.draw()
        basicfont = pygame.font.SysFont('sylfaen', 120)
        texto = basicfont.render('You Win!', True, (0, 255, 0)) #(0, 0, 0))
        textrecto = text.get_rect()
        textrecto.centerx = screen.get_rect().centerx
        textrecto.centery = screen.get_rect().centery
        screen.blit(texto, (400, 360))
        clock.tick(1)
        player_one_score = 0
        player_two_score = 0

    elif player_two_score == 100:
        lose.draw()
        basicfont = pygame.font.SysFont('chiller', 120)
        texte = basicfont.render('You Lose!', True, (255, 0, 0)) #(0, 0, 0))
        textrecte = text.get_rect()
        textrecte.centerx = screen.get_rect().centerx
        textrecte.centery = screen.get_rect().centery
        screen.blit(texte, (400, 360))
        clock.tick(1)
        player_one_score = 0
        player_two_score = 0







    pygame.display.flip()
    clock.tick(60)

Пока что я получаю такие данные, как то, что экран зависает при последней атаке, ждет, а затем показывает код, а когда показывает ваш выигрыш или проигрыш только в течение примерно 1 \ 100-й секунды, что недостаточно, а такжекогда я помещаю его ниже большей части кода, он делает именно то, что нужно

1 Ответ

0 голосов
/ 28 декабря 2018

В двух последних операторах if вы сбрасываете баллы на 0.

player_one_score = 0
player_two_score = 0

Это приводит к тому, что операторы if не выполняются в следующем цикле, поэтому «Вы выиграли» / «Вы проиграли»"знак не рисуется в следующем цикле.Может быть, вы можете сделать кнопку, чтобы перезапустить игру, которая при нажатии сбрасывает счет до 0 и перезапускает игру.

...