Pygame如何摆脱围绕动画的盒子?

时间:2014-07-13 18:51:37

标签: python animation pygame

import pygame



class Sprite:

    def __init__(self, x, y, curren_time):

        self.rect = pygame.Rect(x, y, 100, 110)

        self.images = []

#Tells pygame the image list that you want to stitch together
        for x in range(10):
            img = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST6/Cernunnos" + str(x) +".PNG")
#Append the image to each other to create the animation effect
            self.images.append( img )

        self.current_image = 0

        self.time_num = 100
        self.time_target = curren_time + self.time_num

    def update(self, curren_time):

        if curren_time >= self.time_target:

            self.time_target = curren_time + self.time_num

            self.current_image += 1

            if self.current_image == len(self.images):
                self.current_image = 0

    def render(self, window):

        window.blit(self.images[self.current_image], self.rect)
#Colors
black = (0,0,0)
white = (255,255,255)




def main():

    pygame.init()

    window = pygame.display.set_mode((800,600))

    pygame.display.set_caption("Sprites")

    move_x, move_y = 0, 0

    clock = pygame.time.Clock()
    curren_time = pygame.time.get_ticks()

    player = Sprite(110,100,curren_time)

    font = pygame.font.SysFont(None, 150)
    pause_text = font.render("PAUSE",1,white)
    pause_rect = pause_text.get_rect( center = window.get_rect().center )

#Adding how many places the image moves when using the key function
    state_game = True
    state_pause = False

#So while True
    while state_game:

        curren_time = pygame.time.get_ticks()



        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                state_game = False

            elif event.type == pygame.KEYDOWN:

                if event.key ==pygame.K_ESCAPE:
                    state_game = False

                elif event.key == pygame.K_SPACE:
                    state_pause = not state_pause

                if event.key == pygame.K_LEFT:
                    move_x = -3

                elif event.key == pygame.K_RIGHT:
                    move_x = 3

                elif event.key == pygame.K_UP:
                    move_y = -3

                elif event.key == pygame.K_DOWN:
                    move_y = 3

            elif event.type == pygame.KEYUP:

                if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
                    move_x = 0

                elif event.key in (pygame.K_UP, pygame.K_DOWN):
                    move_y = 0



        if not state_pause:
            player.rect.x += move_x
            player.rect.y += move_y
            player.update(curren_time)

#Fills the window with a color

        window.fill(white)

        player.render(window)

        if state_pause:
            window.blit(pause_text,pause_rect)

        pygame.display.flip()



        clock.tick(50)



    pygame.quit()



if __name__ == '__main__':
    main()

#Code created by furas

我有一个键盘动画它可以工作,除了我遇到的问题是图像周围有一个框有没有办法删除它?代码发布在上面请随意向我展示代码示例,因为我是pygames的新手。

1 个答案:

答案 0 :(得分:2)

图片质量可能会有问题,或者您在加载图片时必须使用convert_alpha()convert()

img = pygame.image.load("...").convert()
img = pygame.image.load("...").convert_alpha()

修改

应该是:

for x in range(10):
    img = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST6/Cernunnos" + str(x) +".PNG").convert_alpha()