简体   繁体   English

Pygame.transform.rotate缩放而不是旋转

[英]Pygame.transform.rotate scales istead rotation

Recently I decided to start my third pygame game.In that game the player should fire at airplanes from the cannon in the bottom of the screen,by pointing to the airplane with the mouse.I putted the code in more modules(more.py files) for easier understanding.I started by trying to get cannon barel rotate towards mouse current position.So here we go. 最近我决定开始我的第三个pygame游戏。在该游戏中,玩家应使用鼠标指向飞机从屏幕底部的加农炮向飞机开火。我将代码放在了更多模块中(more.py文件),以便于理解。我首先尝试使炮弹向鼠标当前位置旋转。

main.py main.py

import pygame,sys
import screen
import constants
from pygame.locals import *
from loop import *

screen.screen = pygame.display.set_mode((800,600))
main_loop()

constatns.py constatns.py

import pygame

pygame.init()

scr = pygame.display.list_modes()
resolution = (scr[0][0],scr[0][1])
angle = 0
barell = pygame.image.load("barell.png")
tux = pygame.image.load("tux.png")
tux2 = pygame.transform.scale(tux,(100,100))

loop.py loop.py

import pygame,event
import screen
import constants
import math
import random
import groups
from faster_barell import *


faster_barell = faster_barell()
faster_barell.add_to_group()

def main_loop():
    while True:
        mx,my = pygame.mouse.get_pos()
        event.Event()
        screen.screen.fill([0,0,0])
        groups.barell_group.draw(screen.screen)
        for t in groups.barell_group:
            dx = mx - t.rect.x
            dy = my - t.rect.y
            angle = math.atan2(dx,dy)
            angle2 = math.degrees(angle)
            constants.angle = angle2
            t.image = pygame.transform.rotate(constants.barell,angle2)

         pygame.display.flip()

screen.py screen.py

import pygame
import constants

pygame.init()

screen = None

event.py event.py

import pygame,sys
from pygame.locals import *

class Event(object):
    def __init__(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

faster_barell.py fast_barell.py

import pygame
import constants
import groups

class faster_barell(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = constants.barell
        self.rect = self.image.get_rect()
        self.rect.x = 750
        self.rect.y = 550

    def add_to_group(self):
        groups.barell_group.add(self)

groups.py groups.py

import pygame

barell_group = pygame.sprite.Group()

Now instead of rotating pygame(I can't really explain how)scales the barell image.The barell image is just a blank(white) 10x30 image).Now here comes even more strange part.When in t.image = pygame.transform.rotate(constants.barell,angle2) I change constants.barell to constants.tux2(which is just a tux image(just for testing it won't be in the game) everything works just fine! Here is the tux image I worked with http://upload.wikimedia.org/wikipedia/commons/3/3e/Tux-G2.png .I tried to solve the problem by changing dx and dy in math.atan2 to something else(dy,dx,-dy,dx,-dx,-dy and so on)please help I'm trying to solve this for about 6 hours(I never ask on stackoverflow unless I really can't do anything to get the code working) 现在而不是旋转pygame(我真的无法解释如何)缩放barell图像.barell图像只是一个空白(白色)10x30图像)。现在这里出现了更奇怪的部分。当在t.image = pygame.transform中时.rotate(constants.barell,angle2)我将constants.barell更改为constants.tux2(这只是一个燕尾服图像(只是为了测试它不会出现在游戏中),一切正常!这是我工作的燕尾服图像我尝试使用http://upload.wikimedia.org/wikipedia/commons/3/3e/Tux-G2.png来解决此问题,方法是将math.atan2中的dx和dy更改为其他内容(dy,dx,-dy ,dx,-dx,-dy等),请帮我解决大约6个小时(我从来没有问过stackoverflow,除非我真的什么都不能使代码正常工作)

Here are some helpful links: docs 以下是一些有用的链接: docs

from the rotate function doc: 从旋转功能文档:

"Unless rotating by 90 degree increments, the image will be padded larger to hold the new size ." “除非以90度为增量旋转,否则图像将被较大地填充以保持新的尺寸 。”

the solution is to crop the image after every rotation. 解决方法是在每次旋转后裁剪图像。 You can crop by writing a small function that will create a new surface, and blit the middle part onto the new surface. 您可以通过编写一个小的函数来进行裁剪,该函数将创建一个新曲面,并将中间部分涂抹到新曲面上。 This problem was already solved here: so 这个问题已经解决了这里: 所以

Thank you for your answers,I tried all of them but none worked for me. 谢谢您的回答,我尝试了全部,但没有一个对我有用。 Instead i fixed it like this 相反,我像这样修复它

pygame.transform.rotozoom(constants.barell,angle2,1) instead of pygame.transform.scale. pygame.transform.rotozoom(constants.barell,angle2,1)而不是pygame.transform.scale。

Thank you all :D 谢谢大家:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM