用精灵躲避不会改变图像

时间:2018-08-12 00:20:24

标签: python pygame

当我用pygame制作的精灵时,图像没有变化,但是精灵确实下降了几个像素,这是应该发生的,因为应该将图像更改为鸭子图像和矩形变小。

import pygame
import random
import os

#Screen
WIDTH = 900
HEIGHT = 600
FPS = 30

#Colours
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# Setting up assets folder
# Path : "C:\Users\Ronit\source\repos\PythonApplication2\Game\img"

game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, "img")
img_player = os.path.join(img_folder, "p1_stand.png")
img_folder2 = os.path.join(img_folder, "p1_left")
img_player2 = os.path.join(img_folder2, "pp1_stand.png")
png_walking = "p1_walk0"
png_walking2 = "pp1_walk0"

class Player(pygame.sprite.Sprite): #Sprite for player
    def __init__(self): # Make the sprite
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(img_player).convert()
        self.image.set_colorkey(BLACK) # this will not show the rectangle colour
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH / 2 , HEIGHT / 2 + 70)
        #track ducking state
        self.ducking = False

    def move_right(self, pixels): # Move the sprite
       self.rect.x += pixels

    def move_left(self, pixels): # Move the sprite
       self.rect.x -= pixels

    def move_up(self, pixels):
        self.rect.y -= pixels
        if(self.rect.y > HEIGHT):
            self.rect.right = 0

    def player_moveRightPNG(self, pose):
        walking_img = os.path.join(img_folder, "%s%s.png" % (png_walking, str(pose)))
        self.image = pygame.image.load(walking_img).convert()
        self.image.set_colorkey(BLACK) # this will not show the rectangle colour

    def player_moveLeftPNG(self, pose):
        walking_img2 = os.path.join(img_folder2, "%s%s.png" % (png_walking2, str(pose)))
        self.image = pygame.image.load(walking_img2).convert()
        self.image.set_colorkey(BLACK) # No rectangle colour

    def player_defaultLeftPNG(self):
        self.image = pygame.image.load(img_player2).convert()
        self.image.set_colorkey(BLACK) # this will not show the rectange colour

    def player_defaultRightPNG(self):
        self.image = pygame.image.load(img_player).convert()
        self.image.set_colorkey(BLACK) # this will not show the rectangle colour

    def player_duckRight(self):
        docking_player = os.path.join(img_folder, "p1_duck.png")
        self.image = pygame.image.load(docking_player).convert()
        self.image.set_colorkey(BLACK)

    def player_duckLeft(self):
        docking_player = os.path.join(img_folder, "pp1_duck.png")
        self.image = pygame.image.load(docking_player).convert()
        self.image.set_colorkey(BLACK)

    def player_stand(self):
        standing = os.path.join(img_folder, "p1_front.png")
        self.image = pygame.image.load(standing)
        self.image.set_colorkey(BLACK)
        #revert the ducking state
        if self.ducking:
            self.ducking = False
            self.rect.y = self.rect.y - 21

    def duckRect(self):
        #set our ducking state
        self.ducking = True
        self.rect.y = self.rect.y + 21

    def walls(self):
        if self.rect.right > WIDTH:
            self.rect.right = WIDTH
        if self.rect.left < 0:
            self.rect.left = 0

def main():

# initialize pygame and create window
    pygame.init() # initilize pygame
    pygame.mixer.init() # initilize sound
    pygame.display.init() # initilize display
    screen = pygame.display.set_mode((WIDTH, HEIGHT)) # Create Window  
    pygame.display.set_caption("A game") #Show on top of the window
    clock = pygame.time.Clock() # Handles the speed and uses FPS
    background_folder = os.path.join(img_folder, "background")
    background = os.path.join(background_folder, "bg.png")
    screen_image = pygame.image.load(background).convert()
    direction = None
    last_direction = None
    cnt = 1
    all_sprites = pygame.sprite.Group()
    player = Player()
    all_sprites.add(player)

# Game Loop (Every game has it)
    running = True
    while running:

    # Keeping the loop running at the right speed
        clock.tick(FPS)

    # Proccess input (events)
        for event in pygame.event.get():
            if (event.type == pygame.QUIT):
                running = False
            elif(event.type == pygame.KEYDOWN):
                if(event.key == pygame.K_q):
                    running = False
        keys = pygame.key.get_pressed()
        if (keys[pygame.K_LEFT]):
            player.move_left(5)
            last_direction = pygame.K_LEFT
            direction = pygame.K_LEFT
        elif (keys[pygame.K_RIGHT]):
            player.move_right(5)
            last_direction = pygame.K_RIGHT
            direction = pygame.K_RIGHT
        elif (keys[pygame.K_UP]):
            player.move_up(5)
            direction = pygame.K_UP
        elif(keys[pygame.K_DOWN]):
            direction = pygame.K_DOWN
        else:
            direction = None

    # Update
        if (direction == pygame.K_RIGHT):
            if (cnt == 12):
                cnt = 1
            player.player_moveRightPNG(cnt % 12)
            cnt += 1
        elif (direction == pygame.K_LEFT):
            if (cnt == 12):
                cnt = 1
            player.player_moveLeftPNG(cnt % 12)
            cnt += 1
        else:
            cnt = 1
            player.player_defaultLeftPNG()

        if (direction == pygame.K_DOWN and last_direction == pygame.K_RIGHT):
           # import pdb; pdb.set_trace()
            #only call duck if we just started ducking
            if not player.ducking:
                player.player_duckRight()
                player.duckRect()
        elif (direction == pygame.K_DOWN and last_direction == pygame.K_LEFT):
            if not player.ducking:
                player.player_duckLeft()
                player.duckRect()
        if(direction == None):
            player.player_stand()

        player.walls()

        all_sprites.update()

        #screen.fill(BLUE)
    # Draw/Render
        screen.blit(screen_image, [0, 0])
        all_sprites.draw(screen)

    # AFTER drawing everything, flip the display - Double Buffering (draws and flips instead of drawing instantly)
        pygame.display.flip()

    pygame.quit()

if __name__ == "__main__":
    main()

如果您在Sprite类中查看我的闪避功能,则应该加载闪避图像,并且它们会加载1帧,然后其余帧是不同的站立图像。在代码的底部,您还可以看到按下DOWN键的更新,并且正在加载躲避图像(取决于精灵看起来的侧面)。

0 个答案:

没有答案