Hi, I've just started using Python/Pygame as a hobby.

I've gotten the code to draw my Plane.png and move it using the wsad keys, and I mapped the q and e keys to rotate it which it does, but each time it rotates, it 'falls' down the screen to the bottom right and after a few key presses, disappears.
I read that this is because when it turns the sprite, it adds padding to resize the image while it is turning. I think that each time it turns, it adds more padding, making it add exponentially for each keypress. I can't figure out how to stop this from happening. Any help for a beginner greatly appreciated!

The Colours import is just a file where I keep my pre-defined colours for easier colour filling, and Plane.gif is an inch by inch and a half gif of a black plane with transparent background, and loads no problem, it just flies away when i use the e or q keys.

import pygame, sys, time, random
from pygame.locals import *
from Colours import *
from PIL import Image, ImageDraw




class MySprite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('Plane.gif').convert()
        self.rect = self.image.get_rect()
        self.x = 0
        self.y = 0

    def draw(self,surface):

            surface.blit(self.image,(self.x,self.y))


    def onKeyPress(self):

        key = pygame.key.get_pressed()
        distance = 5
        if key[ord('s')] or key[pygame.K_DOWN]: #down
            self.y += distance
        elif key[ord('w')] or key[pygame.K_UP]: #up
            self.y -= distance
        if key[ord('d')] or key[pygame.K_RIGHT]: #right
            self.x += distance
        elif key[ord('a')] or key[pygame.K_LEFT]: #left
            self.x -= distance

        rotate = pygame.transform.rotate
        if key[ord('e')]:
            self.image = rotate(self.image, 10)
        elif key[ord('q')]:
            self.image = rotate(self.image, -10)

class Event(object):

    def __init__(self):

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit()


pygame.init()

fps = 30
fpsClock = pygame.time.Clock()

size = width,height = 800,600
screen = pygame.display.set_mode(size)
mySprite = MySprite()

while True:

    event = Event()
    screen.fill(Blue)

    mySprite.draw(screen)
    mySprite.onKeyPress()

    pygame.display.update()

    fpsClock.tick(fps)

There's the code, hope you can help, thanks!

Recommended Answers

All 5 Replies

You have to use an auxiliar image variable, so you rotate the image and saves it in the aux var, and the original image is not modified.

self.image = pygame.image.load('Plane.gif').convert()
self.rotatedImage = rotate(self.image, 10)
surface.blit(self.rotatedImage,(self.x,self.y))

try it and tell us how it worked.
Maybe you will see some strange rotation behavior and it depends on how you managed the center of the image or rect.

Maybe it's similar to the strange effects I encountered here:

# exploring module pygame
# draw a white ellipse on a black screen and move it
# note: rect behaves strange, stretches the ellipse!!!

import pygame as pg

black = (0, 0, 0)

# window/screen width and height
w = 600
h = 500
# default background is black
screen = pg.display.set_mode((w, h))

# set up some of the ellipse specs
# (r, g, b) tuple --> white
color = (255, 255, 255)
# optional width of the ellipse's border
# if width = 0 (or not given) the shape is filled with color
width = 0

for delta in range(0, 180, 3):
    # (x1, y1, x2, y2) upper left and lower right corner
    # coordinates of the rectangle the ellipse is bound by
    # if the recangle forms a square then a circle is drawn
    x1 = 30+delta
    y1 = 20
    x2 = 130+delta
    y2 = 120
    rect = (x1, y1, x2, y2)
    print(x1, y1, x2, y2)  # test seems okay
    # this erases the old sreen with black
    screen.fill(black)
    pg.draw.ellipse(screen, color, rect, width)
    # update screen
    pg.display.flip()
    milliseconds = 50
    pg.time.delay(milliseconds)

# event loop ...
running = True
while running:
    for event in pg.event.get():
        # quit when window corner x is clicked
        if event.type == pg.QUIT:
            running = False

It doesn't change if you have delta only for x1, but rect position turns weird.

I forgot to say in my previous post that you have to obtain the rect from each rotated image and set
self.rect.center = [self.x, self.y] on each step.
Well, try it, maybe I'm doing something unnecessary.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.