遇到pygame.translation.rotation问题

时间:2014-02-26 18:57:33

标签: python pygame

所以我正在测试赛车游戏的轮换。 这是代码......

import pygame
from pygame.locals import *

pygame.init()
mainClock = pygame.time.Clock()
degree = 0
WHITE = 250,250,250
rect2 = pygame.rect = (100,100,50,50)
WINDOWWIDTH = 1200
WINDOWHEIGHT = 750

thing = pygame.image.load('thing.bmp')
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Teh test')

while True:
    rect2 = pygame.rect = (100,100,50,50)
    degree += 2

    if degree >= 50:
        degree = 0

    screen.fill((40, 40, 40))
    thing = pygame.transform.rotate(thing,degree)
    pygame.draw.rect(screen,WHITE,rect2)
    screen.blit(thing,(100,100))
    pygame.display.update()
    mainClock.tick(60)

所以问题是,当我运行它时,rect变得巨大,图像旋转离开屏幕。我究竟做错了什么? (答案越多越好。)谢谢!

(对该版本使用python 3.3和pygame。)

1 个答案:

答案 0 :(得分:0)

您不应该覆盖thing。这样你累积度数。

首先你将thing旋转2度,而不是旋转thing从前一次旋转4度+2度= 6度,而不是旋转thing从前一轮旋转6度+4度与前一轮相差+2度= 12度等。

degee >= 50得到degee = 0但您仍然轮换thing(50 + 48 + 46 + ... + 2)时

使用thing2

import pygame
from pygame.locals import *

#----------------------------------------------------------------------
# constants 
#----------------------------------------------------------------------

WHITE = 250,250,250
WINDOWWIDTH = 1200
WINDOWHEIGHT = 750

#----------------------------------------------------------------------
# game
#----------------------------------------------------------------------

pygame.init()
mainClock = pygame.time.Clock()

screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Teh test')

#thing = pygame.image.load('thing.bmp')
thing = pygame.image.load('player.png')

rect2 = pygame.rect = (100,100,50,50)

degree = 0

#----------------------------------------------------------------------
# mainloop
#----------------------------------------------------------------------

running = True
while running:

    # events

    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False

    # calculations

    degree += 2

    if degree >= 50:
        degree = 0

    thing2 = pygame.transform.rotate(thing, degree)

    # draws

    screen.fill((40, 40, 40))
    pygame.draw.rect(screen,WHITE,rect2)
    screen.blit(thing2,(100,100))

    pygame.display.update()

    # clock

    mainClock.tick(30)

#----------------------------------------------------------------------
# exit
#----------------------------------------------------------------------

pygame.quit()
BTW:永远不会覆盖原始图像,因为旋转甚至360度的图像永远不会像边缘那样漂亮。


我在代码'player.png'

中使用的PNG图像

enter image description here

游戏中的

PNG(Python 2.7,Linux Mint 16)

enter image description here