使用绘图功能时,pygame精灵不会在屏幕上闪烁

时间:2018-11-26 17:39:12

标签: python pygame

我正在尝试制作一个游戏,其中有两个玩家互相射击。我的一位来自某班的球员成功地出现在屏幕上。但是第二个玩家来自另一个职业,实际上是另一个具有相同功能的职业的复制品。但以某种方式不起作用:

import pygame
import random
import sys

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


class Background:
    picture = pygame.image.load("C:/images/cliff.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_first:
    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()


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

    def draw(self):      #left right
        #screen.blit(pygame.transform.flip(self.picture, True, False), self.rect)
        screen.blit(self.picture, (self.xpos, self.ypos))


class player_second:
    picture = pygame.image.load("C:/aliens/Giantmechanicalcrab2.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()


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

    def draw(self):      #left right
        #screen.blit(pygame.transform.flip(self.picture, True, False), self.rect)
        screen.blit(self.picture, (self.xpos, self.ypos))







class player_one_Bullet(pygame.sprite.Sprite):
    picture = pygame.image.load("C:/aliens/giphy.gif").convert_alpha()
    picture = pygame.transform.scale(picture, (100, 100))


    def __init__(self):
        self.xpos = 360
        self.ypos = 360
        self.speed_x = 0
        super().__init__()
        self.rect = self.picture.get_rect()


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

    def draw(self):
        screen.blit(self.picture, (self.xpos, self.ypos))
        #self.screen.blit(pygame.transform.flip(self.picture, False, True), self.rect)

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



player_one = player_first(0, 0)
player_two = player_second(1280, 0)
cliff = Background(0, 0)
player_one_bullet_list = pygame.sprite.Group()

while True:

    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 = player_one_Bullet()

                    player_one_bullet.ypos = player_one.ypos
                    player_one_bullet.xpos = player_one.xpos

                    player_one_bullet.speed_x = 14

                    player_one_bullet_list.add(player_one_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


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




    player_one.update()
    player_two.update()
    cliff.draw()
    player_one.draw()
    player_two.draw()

    player_one_bullet_list.update()

    for player_one_bullet in player_one_bullet_list:
            player_one_bullet.draw()



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



#IGNORE THIS 
#player_one_bullet_list = pygame.sprite.Group()



#elif event.key == pygame.K_SPACE:
#                    player_one_bullet = player_one_Bullet()
#
#                    player_one_bullet.ypos = player_one.ypos
#                    player_one.xpos = player_one.xpos
#
#                    player_one_bullet.speed_x = 14
#
#                    player_one_bullet_list.add(player_one_bullet)
#
#    
#player_one_bullet_list.update()
#
#    for player_one_bullet in player_one_bullet_list:
#           

player_one_bullet.draw()

0 个答案:

没有答案