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

Re: [pygame] Moving sprites.



Okay that was kind of a stupid question, haha. Sorry. I got it. Just needed to check before it was moved. Duh.

On Sun, Jul 27, 2008 at 2:28 PM, Percival Kelley <percivalkelley@xxxxxxxxx> wrote:
Alright, one more question for you guys if you don't mind.

I got the sprites working properly, now I'm implementing collision detection. I only want to move my sprite if it's not touching another sprite in the enemy group.

I am using this:

        if self.event.type == KEYDOWN: #look for key presses for up/down/left/right arrow keys and move appropiately
            n1 = self.ship.loc1
            n2 = self.ship.loc2
            if self.event.key == K_UP or self.event.key == K_w:
                n2 -= 7
            if self.event.key == K_DOWN or self.event.key == K_s:
                n2 += 7
            if self.event.key == K_RIGHT or self.event.key == K_d:
                n1 += 7
            if self.event.key == K_LEFT or self.event.key == K_a:
                n1 -= 7
               
            if not pygame.sprite.spritecollide(self.ship, self.egroup, False):
           
                if n1 < 475 and n1 > 5 and n2 < 475 and n2 > 5: #boundaries
                   
                    if n1 != self.ship.loc1 or n2 != self.ship.loc2:
                        self.ship.loc1 = n1 #set locs to n so it updates on next move
                        self.ship.loc2 = n2
                        self.ship.rect.center = [self.ship.loc1, self.ship.loc2] #move the sprite
                        print self.ship.rect.center #print data for our cmd prompt
                        self.sgroup.clear(self.screen, self.bg) #erase the old stuff
                        rectlist = self.sgroup.draw(self.screen) #draw the new stuff
                        pygame.display.update(rectlist) #display it

Now, it works, but as soon as I bump into the other sprite, it locks up. Obviously it's because by the time its getting triggered, it's already stuck. I'm not sure how to handle this. Any ideas?


On Sun, Jul 27, 2008 at 1:33 PM, Paulo Silva <nitrofurano@xxxxxxxxx> wrote:
you welcome! :-) - btw, i'm still trying to to on Pygame what i were
doing on sdlBasic up to now
(http://nitrofurano.linuxkafe.com/sdlbasic), stuff like how to get
lots of sprites from just one picture, etc.

------

On Mon, Jul 28, 2008 at 12:19 AM, Percival Kelley
<percivalkelley@xxxxxxxxx> wrote:
> Awesome. Thank you. That did the trick.
>
> On Sun, Jul 27, 2008 at 1:10 PM, Paulo Silva <nitrofurano@xxxxxxxxx> wrote:
>>
>> well, for now i were enjoying these ones
>> http://www.cs.iupui.edu/~aharris/pygame/ch04/
>>
>> --------
>>
>> On Sun, Jul 27, 2008 at 11:38 PM,  <percivalkelley@xxxxxxxxx> wrote:
>> > So I've read piman's tutorial and checked out a few others. I'm still
>> > confused. I don't really understand the process of moving a sprite
>> > around. I would like to use RenderUpdates and such, but I'm lost.
>> >
>> > I basically have a simple script that moves a ship around a background
>> > (the background is an image.)
>> >
>> > To create the sprite I'm using:
>> >
>> > class Units(pygame.sprite.Sprite):
>> >
>> >        def __init__(self, img, loc1 = 250, loc2 = 250):
>> >                pygame.sprite.Sprite.__init__(self) #start up sprites
>> >
>> >                #locs
>> >                self.loc1 = loc1
>> >                self.loc2 = loc2
>> >
>> >                #create sprite
>> >                self.image, self.rect = load_image(img)
>> >                self.rect.center = [self.loc1, self.loc2]
>> >
>> > I create the ship like this:
>> > self.ship = Units('bship.png')
>> > self.screen.blit(self.ship.image, self.ship.rect)
>> >
>> > To redraw the ship when it moves I'm using this:
>> >
>> > self.ship.loc1 = n1 #n1 and n2 are setup earlier in the code that
>> > looks for key presses and sets the pixel difference
>> > self.ship.loc2 = n2
>> > self.ship.rect.center = [self.ship.loc1, self.ship.loc2]
>> > print self.ship.rect.center
>> > self.screen.blit(self.bg, (0,0))
>> > self.screen.blit(self.ship.image, self.ship.rect.center)
>> > pygame.display.flip()
>> >
>> > I'm really pretty confused at this point. I know I'm just doing
>> > something stupid, but I haven't managed to get any other code working
>> > to redraw the ship properly when it's moved. I don't have the code
>> > from when I was doing it wrong, but basically, can someone point me in
>> > the right direction to changing this over to just redraw the
>> > appropriate areas?
>> >
>> > Thanks, and sorry this was rambling.
>> >
>
>