[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

RE: [pygame] possible to get rid of ghost image after rotating aline?



Once you rotate a surface it will not be the same size as the original object if the source is not a square, so collision detection would actually probably be harder than detecting if a line intersected a rectangle or a circle.

From: tom arnall
Sent: â8/â25/â2015 6:25 PM
To: pygame-users@xxxxxxxx
Subject: Re: [pygame] possible to get rid of ghost image after rotating aline?

I spoke too soon. the use of draw.line() won't work for my application.

I need to be able to easily detect when the line touches a moving
target. Therefore AFIK I need the line to be either a surface or drawn
on a surface, so I can use the surface Rect to check when the  line
collides with a target. I can't see a way to use a line created by
draw.line() to do this.

I also would like the line to behave like the example you referenced,
i.e. to rotate around one of its endpoints, not its center, but this
is a nice-to-have feature; it would also work if the line surface just
rotated around its center.

Tom Arnall

.....
âI have no special talents, only a passionate and stubborn curiosity.â
Albert Einstein





On 8/25/15, Paul Vincent Craven <paul@xxxxxxxxxxxxxxxx> wrote:
> I'm not sure why you are creating a new surface everytime. Or why you
> create one at all?
>
> Is this similar to what you are looking for?
>
> http://programarcadegames.com/python_examples/f.php?file=radar_sweep.py
>
> Paul Vincent Craven
>
> On Tue, Aug 25, 2015 at 3:57 PM, tom arnall <kloro2006@xxxxxxxxx> wrote:
>
>> I want to rotate a line around a point. The code that follows does
>> this, BUT when it draws a line, it leaves next to the line its ghost.
>> Is this a problem with pygame, or is there a change I can make in the
>> code which will make the problem go away?
>>
>>
>> import pygame, sys
>> from pygame.locals import *
>> import time
>>
>> pygame.init()
>>
>> #create a surface that will be seen by the user
>> screen =  pygame.display.set_mode((400, 400))
>>
>> #create a varible for degrees pf rotation
>> degree = 0
>> while True:
>>
>>     for event in pygame.event.get():
>>         # quit the game if escape is pressed
>>         if event.type == QUIT:
>>             sys.exit()
>>         elif event.type == KEYDOWN and\
>>                 event.key == K_ESCAPE:
>>             sys.exit()
>>
>>     #clear screen at the start of every frame
>>     screen.fill((40, 40, 40))
>>
>>     #create new surface with white BG
>>     surf =  pygame.Surface((1000, 5))
>>     surf.fill((255, 255, 255))
>>     #set a color key for blitting
>>     surf.set_colorkey((255, 0, 0))
>>
>>
>>     ##ORIGINAL UNCHANGED
>>     #where will the static image be placed:
>>     where = (0,200)
>>
>>     #draw surf to screen and catch the rect that blit returns
>>     blittedRect = screen.blit(surf, where)
>>
>>     ##ROTATED
>>     #get center of surf for later
>>     oldCenter = blittedRect.center
>>
>>     #rotate surf by DEGREE amount degrees
>>     rotatedSurf =  pygame.transform.rotate(surf, degree)
>>
>>     #get the rect of the rotated surf and set it's center to the
>> oldCenter
>>     rotRect = rotatedSurf.get_rect()
>>     rotRect.center = oldCenter
>>
>>     #draw rotatedSurf with the corrected rect so it gets put in the
>> proper
>> spot
>>     screen.blit(rotatedSurf, rotRect)
>>
>>     #change the degree of rotation
>>     degree += 5
>>     if degree > 360:
>>         degree = 0
>>
>>     #show the screen surface
>>     pygame.display.flip()
>>
>>     #wait 60 ms until loop restart
>>     pygame.time.wait(60)
>>
>