创建一个延迟,以使一次显示一颗子弹

时间:2018-12-16 14:55:31

标签: python time pygame sleep collision

我想尝试延迟一下,所以当您按下空格键时,它将发射子弹,然后它将一直弹到它离开屏幕或击中播放器为止(一次仅发射一枚子弹)。我也想要这样做,所以当屏幕上出现项目符号,并且您尝试按空格键时,由于等待而不会发生任何事情,我已经在线查看,并且这些都完全停止了我不希望的游戏:

您能通过代码将解决方案发送给我吗?

import pygame
import sys
import pygame.freetype


pygame.init()
pygame.font.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 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

    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

    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, 500)
cliff = Background(0, 0)
player_one_bullet_list = pygame.sprite.Group()
player_two_bullet_list = pygame.sprite.Group()
player_one_bullet = None
player_two_bullet = None
count = 100
wait = 100
locked = True
lockered = True
player_one_score = 0
player_two_score = 0
loaded = True
ready = True

while True:
    tik_tok = pygame.time.get_ticks()
    print(tik_tok)

    basicfont = pygame.font.SysFont('rubik', 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('rubik', 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:
                player_one_bullet = Bullet_player_one(player_one, player_one.xpos, player_one.ypos, 14)
                player_one_bullet_list.add(player_one_bullet)






            elif event.key == pygame.K_KP0:
                player_two_bullet = Bullet_player_two(player_two, player_two.xpos, player_two.ypos, -14)
                player_two_bullet_list.add(player_two_bullet)







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

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

    player_one_bullet_list.update()
    player_two_bullet_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 bullet in player_one_bullet_list:
        if bullet.is_collided_with(player_two):
            player_one_bullet.kill()
            player_one_score +=1


    for bullet in player_two_bullet_list:
        if bullet.is_collided_with(player_one):
            player_two_bullet.kill()
            player_two_score +=1

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

0 个答案:

没有答案