Pygame项目符号列表。 pygame.Sprite.Group。项目符号不起作用

时间:2018-11-25 17:15:28

标签: python pygame

项目中的飞船没有发射子弹。项目符号列表的值为pygame.sprite.group,但它无法执行添加项目符号或更新之类的操作。我做了另一个非常相似的项目(在我的页面上)。另一个项目里面有pygame.sprite.group并且可以正常工作。为什么这不是呢?

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 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_first(1280, 0)
cliff = Background(0, 0)
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:
                    bullet = Bullet

                    bullet.ypos = player_one.ypos
                    bullet.xpos = player_one.xpos

                    bullet.speed_x = 14

                    bullet_list.add(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()

    bullet_list.update()

    for bullet in bullet_list:
            bullet.draw()



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

1 个答案:

答案 0 :(得分:0)

以下是您的代码有问题的列表:

  • 按下空格键时,您不会创建Bullet的实例,因为您忘记了实际调用该类的操作(又错过了())。

  • Background类似乎毫无用处。您可以使用Sprite来利用pygame的内置功能(例如使用Group)或将单个图像用作LayeredUpdates组的背景。除此之外,创建只对一张图像只使用一次的类是过大的。

  • 您的类player_firstplayer_second是相同的。无需创建仅在数据上不同的不同类别的对象。使用单个类,并将图像的文件名作为参数传递。

  • 您已经为Sprite类使用了Bullet。您为什么不也将它用于播放器类?

  • 您将Sprite子类化为Bullet,但是Sprite应该具有image属性,才能与Group实例一起使用。

  • 您不需要在draw中使用Sprite函数。

  • 您不需要xposypos之类的属性,因为您将Sprite的位置存储在其rect属性中(同样,与Group及其子类一起使用。