0

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.

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

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

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

import pygame
import constants

pygame.init()

screen = None

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

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

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)

4

2 回答 2

0

感谢您的回答,我尝试了所有这些,但没有一个对我有用。相反,我像这样修复它

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

谢谢大家 :D

于 2013-08-04T17:46:47.747 回答
0

以下是一些有用的链接: 文档

来自旋转功能文档:

“除非旋转 90 度增量,否则图像将被填充更大以保持新尺寸。”

解决方案是在每次旋转后裁剪图像。您可以通过编写一个创建新曲面的小函数进行裁剪,并将中间部分粘贴到新曲面上。这个问题已经在这里解决了:所以

于 2013-08-03T15:07:56.737 回答