使精灵移动,使GIF工作并以其他方式切换精灵

时间:2018-11-23 22:02:08

标签: python pygame gif

我正在尝试制作一个游戏,让您互相发射子弹(有两个玩家),但有一些问题,例如我想让子画面面对另一面。我要使GIF正常工作,并且要使播放器更新并绘制到屏幕上。这是我的代码:

我正在尝试制作一个游戏,让您互相发射子弹(有两个玩家),但有一些问题,例如我想让子画面面对另一面。我要使GIF正常工作,并且要使播放器更新并绘制到屏幕上。这是我的代码:

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_1:
    picture = pygame.image.load("C:/aliens/Giantmechanicalcrab2.gif")
    picture = pygame.transform.scale(picture, (200, 200))

    def __init__(self, x, y):
        self.ypos = y
        self.xpos = x
        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_2:
    picture = pygame.image.load("C:/aliens/Giantmechanicalcrab2.gif")
    picture = pygame.transform.scale(picture, (200, 200))

    def __init__(self, x, y):
        self.ypos = y
        self.xpos = x
        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))
        #screen.blit(pygame.transform.flip(self.picture, True, False), self.rect)


class Bullet_player_1(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)


class Bullet_player_2(pygame.sprite.Sprite):
    picture = pygame.image.load("C:/aliens/MajesticLavishBackswimmer-size_restricted.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, True, False), self.rect)

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


player_one_bullet_list = pygame.sprite.Group()
#player_two_bullet_list = pygame.sprite.Group()
player_one = player_1(0, 0)
#player_two = player_2(0, 720)
cliff = Background(0, 0)

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:
                    bullet_player_one = Bullet_player_1
                    bullet_player_one.ypos = player_one.ypos
                    bullet_player_one.xpos = player_one.xpos
                    bullet_player_one.speed_x = 14
                    player_one_bullet_list.add(bullet_player_one)

                #elif event.key == pygame.K_KP0:
                    #bullet_player_two = Bullet_player_2
                    #bullet_player_two.ypos = player_two.ypos
                    #bullet_player_two.xpos = player_two.xpos
                    #bullet_player_two.speed_x = 14
                    #player_two_bullet_list.add(bullet_player_two)

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_d and player_one.speed_x > 0:
                    player_one.speed_x = 0
                elif event.key == pygame.K_a and player_one.speed_x < 0:
                    player_one.speed_x = 0
                #elif event.key == pygame.K_UP and player_two.speed_x > 0:
                    #player_two.speed_x = 0
                #elif event.key == pygame.K_DOWN and player_two.speed_x < 0:
                    #player_two.speed_x = 0

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

    player_one_bullet_list.update()
    #player_two_bullet_list.update()

    for bullet_player_one in player_one_bullet_list:
            bullet_player_one.draw()

    #for bullet_player_two in player_two_bullet_list:
            #bullet_player_two.draw()

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

0 个答案:

没有答案