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)