旋转没有显示

时间:2017-12-06 22:38:41

标签: python image rotation pygame sprite

我试图让“播放器”旋转。这是头部需要可旋转的图片。但错误是头部没有显示(播放器没有显示)。图像(bigshaghoofdz.png)需要是可旋转的。 按K_UP和K_DOWN可旋转。可以转360度。最后它需要从它的眼睛射出激光。

import pygame
import random
import math
GRAD = math.pi / 180

class Config(object):
    fullscreen = False
    width = 640
    height = 360
    fps = 60

class Player(pygame.sprite.Sprite):
    maxrotate = 360 
    down = (pygame.K_DOWN)
    up = (pygame.K_UP)
    number = 1

    def __init__(self, startpos = (-1,223), angle=0):
        self.number = Player.number
        Player.number += 1
        Player.book[self.number] = self
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.pos = [startpos[-1], startpos[223]]
        self.down = Player.down[self.number]
        self.up = Player.up[self.number]

        image = pygame.image.load('BigShagHoofdz.png')

    def update(self, seconds):

        pressedkeys = pygame.key.get_pressed()          

        self.turndirection = 0 
        if pressedkeys[self.down]:
            self.turndirection -= 1
        if pressedkeys[self.up]:
            self.turndirection += 1

#end classes
#defs

def radians_to_degrees(radians):
    return (radians / math.pi) * 180.0

def degrees_to_radians(degrees):
    return degrees * (math.pi / 180.0)

def main():
    #game 
    pygame.init()
    screen=pygame.display.set_mode((Config.width,Config.height))
    background = pygame.image.load('background.jpg')

    background = background.convert()
    screen.blit(background, (0,0))
    clock = pygame.time.Clock()
    FPS = Config.fps
    playtime = 0

    Playergroup = pygame.sprite.Group()
    allgroup = pygame.sprite.LayeredUpdates()

    Player.groups = Playergroup, allgroup

    mainloop = True
    while mainloop:
        millisecond = clock.tick(Config.fps)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                mainloop = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    mailoop = False 

        pygame.display.set_caption("say hi")
        allgroup.clear(screen, background)
        allgroup.draw(screen)
        pygame.display.flip()
    return 0

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

如果按下某个键,您可以增加角度,然后转换原始图像,将其指定给self.image属性,并获得一个带有旧矩形中心的新矩形。

import pygame


class Config(object):
    fullscreen = False
    width = 640
    height = 360
    fps = 60

class Player(pygame.sprite.Sprite):
    maxrotate = 360
    down = (pygame.K_DOWN)
    up = (pygame.K_UP)

    def __init__(self, startpos=(100,223), angle=0):
        super().__init__()
        self.pos = list(startpos)
        self.image = pygame.Surface((70, 70), pygame.SRCALPHA)
        pygame.draw.circle(self.image, pygame.Color('dodgerblue'), (35, 35), 35)
        pygame.draw.circle(self.image, pygame.Color('black'), (35, 15), 8)
        self.orig_image = self.image
        self.rect = self.image.get_rect(center=startpos)
        self.angle = angle

    def update(self, seconds):
        pressedkeys = pygame.key.get_pressed()
        if pressedkeys[self.down]:
            self.angle -= 2
            self.rotate_image()
        if pressedkeys[self.up]:
            self.angle += 2
            self.rotate_image()

    def rotate_image(self):
        self.image = pygame.transform.rotate(self.orig_image, self.angle)
        self.rect = self.image.get_rect(center=self.rect.center)


def main():
    pygame.init()
    screen=pygame.display.set_mode((Config.width,Config.height))
    clock = pygame.time.Clock()
    FPS = Config.fps
    allgroup = pygame.sprite.LayeredUpdates()
    player = Player()
    allgroup.add(player)

    mainloop = True
    while mainloop:
        millisecond = clock.tick(Config.fps)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                mainloop = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    mailoop = False

        allgroup.update(millisecond)

        screen.fill((50, 50, 70))
        allgroup.draw(screen)
        pygame.display.flip()


if __name__ == '__main__':
    main()
    pygame.quit()