在pygame中为玩家精灵创建子弹

时间:2018-11-15 20:56:42

标签: python pygame

我正在尝试在pygame中创建项目符号,但是我不知道该怎么做。我已经设法走上正轨,但我只知道这些。我看过很多关于使用sprite.group以及创建列表的教程,但是我不确定如何做到这一点。

'''     导入pygame     随机导入     导入系统

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

GRAVITY = .9  # Define this in the global scope

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

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

    def draw(self):
        # Blit the picture onto the screen surface.
        # `self.picture` not just `picture`.
        screen.blit(self.picture, (self.xpos, self.ypos))


class Monster:
    picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
    picture = pygame.transform.scale(picture, (200, 200))

    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y
        # If you want to move continuously you need to set these
        # attributes to the desired speed and then add them to
        # self.xpos and self.ypos in an update method that should
        # be called once each frame.
        self.speed_x = 0
        self.speed_y = 0
        self.on_ground = True

    def update(self):
        # Call this method each frame to update the positions.
        #self.xpos += self.speed_x
        #self.ypos += self.speed_y

        self.speed_y += GRAVITY  # Accelerate downwards.
        self.xpos += self.speed_x 
        self.ypos += self.speed_y

        # Stop falling at the bottom of the screen.
        if self.ypos >= 520: #if the position is more than 520 then it will make the ypos 520 and there will be be no y_speed 
            self.ypos = 520
            self.speed_y = 0
            self.on_ground = True #this means that you are on the ground

    #def change (self, costume):
        #if costume == 0:
            #picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
            #picture = pygame.transform.scale(picture, (200, 200))
        #elif costume == 1:
            #picture = pygame.image.load("C:/pics/hammerhood_jump.png").convert_alpha()
            #picture = pygame.transform.scale(picture, (200, 200))


    # Not necessary anymore.
#     def move_left(self):
#         self.xpos -= 5  # -= not = -5 (augmented assignment).
# 
#     def move_right(self):
#         self.xpos += 5  # += not = +5 (augmented assignment).

    def jump(self):
        if self.on_ground:  # This prevents air jumps. AND WHEN THE FUNCTION IS CALLED IT CHECKS IF YOU ARE ON THE GROUND IF YOU ARE YOU ARE ALLOWED TO JUMP BUT IF YOU ARE IN THE AIR IT WILL NOT WORK
            self.on_ground = False #whenever you call this function it checks wether you are on teh gorund or not it can happen if on_ground is true or false
            #then on_ground value becomes flase meaning that you are not on the ground and then the y_speed goes backwards 25 times for 9 in each
            self.speed_y = -25


        #self.ypos =- 1
        #self.ypos =+ 1
        #self.ypos =- 10
        #self.ypos =+ 10

        # What do you want to do here?
        #for x in range(1, 10):
            #self.ypos -= 1  # -= not =-
            # pygame.display.show()  # There's no show method.

        #for x in range(1, 10):
            #self.ypos += 1
            # pygame.display.show()

    def jump_costume(self):
        picture = pygame.image.load("C:/pics/hammerhood_jump.png").convert_alpha()
        picture = pygame.transform.scale(picture, (200, 200))

    def default_costume(self):
        picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
        picture = pygame.transform.scale(picture, (200, 200))

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






class Enemy:  # Use upper camelcase names for classes.
    picture = pygame.image.load("C:/pics/dangler_fish.png").convert_alpha()
    picture = pygame.transform.scale(picture, (200, 200))

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

    def teleport(self):
        self.xpos = random.randint(1, 1280)
        self.ypos= random.randint(1, 720)

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


class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        self.xpos = 360
        self.ypos = 360
        self.speed_x = 0
        super().__init__()

    picture = pygame.image.load("C:/pics/magma_ball.png").convert_alpha()
    picture = pygame.transform.scale(picture, (200, 200))

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

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

# Create the instances before the while loop.
ice = Background(0, 0)  # I pass 0, 0 so that it fills the whole screen.
hammerhood = Monster(200, 500)
fish = Enemy(0, 0)
bullet_list = []
while True:
    # Handle events.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        # Check if the `event.type` is KEYDOWN first.
        elif event.type == pygame.KEYDOWN:
            # Then check which `event.key` was pressed.
            if event.key == pygame.K_d:
                hammerhood.speed_x = 5
            elif event.key == pygame.K_a:
                hammerhood.speed_x = -5
            elif event.key == pygame.K_w:
                hammerhood.jump()
                hammerhood.jump_costume()
                #hammerhood.change(1)
                #hammerhood.ypos =- 10
                #hammerhood.ypos =+ 10#

            elif event.key == pygame.K_SPACE:
                bullet = Bullet()

                bullet.xpos = hammerhood.speed_x
                bullet.ypos = hammerhood.speed_y

                bullet.speed_x = 12

                bullet_list.add(bullet)
        elif event.type == pygame.KEYUP:
            # Stop moving when the keys are released.
            if event.key == pygame.K_d and hammerhood.speed_x > 0:
                hammerhood.speed_x = 0
            elif event.key == pygame.K_a and hammerhood.speed_x < 0:
                hammerhood.speed_x = 0

    #if hammerhood.xpos == 1280 or hammerhood.xpos == 0:
        #hammerhood.speed_x = 0
    # Update the game.

    bullet_list.update()
    for bullet in bullet_list:
        bullet.draw(screen)
    hammerhood.update()
    fish.teleport()
    #bullet_list.update()
    #for bullet in bullet_list:
        #bullet.draw(screen)

    # Draw everything.
    ice.draw()  # Blit the background to clear the screen.
    hammerhood.draw()
    fish.draw()
    pygame.display.flip()
    clock.tick(60)  # Limit the frame rate to 60 FPS.'''

0 个答案:

没有答案