11 Replies - 3080 Views - Last Post: 07 May 2012 - 10:47 AM Rate Topic: -----

#1 Zephr  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 01-May 12

[Pygame] 2D side scroller getting a sprite to shoot a simple bullet?

Posted 01 May 2012 - 12:28 PM

Hey, I've been working on my own 2D sidescroller for a while now, I have most of it working. Today I decided to add some bullets into the game but I cant seem to get them to work. They'll be basic functionality really; just firing from the player sprites position in a straight line. (I've not added collision detection just yet) Here's my main and my BubbleBullet class, separate from the main. (want to have modules for easy editing later on down the line as I learn more :) ) Help in the right direction would be appreciated.


Main:


import pygame, motherWhale, scoreBoard, babyWhale, seaMonster, bubble
pygame.init()


def main():
    
    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption("Bubble Battle")

    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((255, 255, 255))
    screen.blit(background, (0, 0))
    
    
    #pygame.mixer.music.load('')
    #pygame.mixer.music.play(-1, 0.0)
    #musicPlaying = True
    
    motherwhale = motherWhale.MotherWhale()
    babywhale = babyWhale.BabyWhale()
    monster1 = seaMonster.SeaMonster1()
    monster2 = seaMonster.SeaMonster2()
    bubblebullet = bubble.BubbleSprite(0, 480, 'bubble.png')
    #cloud3 = Cloud()
    #seaworld = seaWorld.SeaWorld()
    scoreboard = scoreBoard.Scoreboard()

    goodSprites = pygame.sprite.Group(motherwhale, babywhale, bubblebullet) #seaworld, island, plane)
    badSprites = pygame.sprite.Group(monster1, monster2)
    #backroundSprite = pygame.sprite.Group(seaworld) 
    scoreSprite = pygame.sprite.Group(scoreboard)

    clock = pygame.time.Clock()
    keepGoing = True
    while keepGoing:
        clock.tick(30)
        
        
        
        #pygame.mouse.set_visible(False)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
                
                
                if bubblebullet.y < 479 and bubblebullet.y > 0:
                    bubblebullet.render()
                    bubblebullet.x -= 5
                
                            
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE or event.key == pygame.K_q:
                    keepGoing = False
                    
                if event.key == pygame.K_UP:
                    motherwhale.y -= 25   
                    
                if event.key == pygame.K_DOWN:
                    motherwhale.y += 25
                    
                if event.key == pygame.K_SPACE:
                    bubblebullet.x = motherwhale.x
                    bubblebullet.y = motherwhale.y
                    
                #if event.key == pygame.K_SPACE:
                   # bubble.x = motherwhale.x
                    #bubble.y = motherwhale.y
                    
                #if event.key == pygame.K_RIGHT:
                    #motherWhale.x += 20
                    
                #if event.key == pygame.K_LEFT:
                    #motherWhale.x -= 20
                    
                        
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_UP:                    #WHEN KEYPRESS LIFTED, STOP SPRITE
                    motherwhale.speed = 0
                    
                elif event.key == pygame.K_DOWN:
                    motherwhale.speed = 0
                               
                    
                if motherwhale.y >= screen.get_width():
                    motherWhale.speed = 0
                    
                if motherwhale.y >= screen.get_height():
                    motherWhale.speed = 0
                    '********************E'
                    
                    
                #if bubble.y < 479 and bubble.y > 0:
                    #bubble.render()
                    #bubble.y -= 5
                    
                    
                    
        #TODO: change to sprite.collideany
        
        #check collisions
        
        if motherwhale.rect.colliderect(babywhale.rect):
            #plane.sndYay.play()
            babywhale.reset()
            scoreboard.score += 1000

        collideMonster = pygame.sprite.spritecollide(motherwhale, badSprites, False)
        if collideMonster:
            #plane.sndThunder.play()
            scoreboard.lives -= 1
            if scoreboard.lives <= 0:
                keepGoing = False
            for monster in collideMonster:
                monster.reset()
        

        goodSprites.clear(screen, background)
        #backroundSprite.clear(screen, background)
        badSprites.clear(screen, background)
        scoreSprite.clear(screen, background)
                    
        goodSprites.update()
        #backroundSprite.update()
        badSprites.update()
        scoreSprite.update()
        
        goodSprites.draw(screen)
        #backroundSprite.draw(screen)
        badSprites.draw(screen)
        scoreSprite.draw(screen)
        
        pygame.display.flip()

 

    
if __name__ == "__main__":
    main()






And my bullet class, 'bullet':

import pygame
pygame.init()

screen = pygame.display.set_mode((1280, 700))

class BubbleSprite:
    def __init__(self, xpos, ypos, filename):
        self.x = xpos
        self.y = ypos
        self.bitmap = pygame.image.load(filename)
        self.bitmap.set_colorkey((0,0,0))
    def set_position(self, xpos, ypos):
        self.x = xpos
        self.y = ypos
    def render(self):
        screen.blit(self.png, (self.x, self.y))






Error I'm getting:

Quote

Traceback (most recent call last):
File "I:\College\Second Year\Game Development\Projects\Sem1Project\src\main.py", line 150, in <module>
main()
File "I:\College\Second Year\Game Development\Projects\Sem1Project\src\main.py", line 34, in main
goodSprites = pygame.sprite.Group(motherwhale, babywhale, bubblebullet) #seaworld, island, plane)
File "C:\Python27\lib\site-packages\pygame\sprite.py", line 552, in __init__
self.add(*sprites)
File "C:\Python27\lib\site-packages\pygame\sprite.py", line 378, in add
sprite.add_internal(self)
AttributeError: BubbleSprite instance has no attribute 'add_internal'


Is This A Good Question/Topic? 1
  • +

Replies To: [Pygame] 2D side scroller getting a sprite to shoot a simple bullet?

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: [Pygame] 2D side scroller getting a sprite to shoot a simple bullet?

Posted 01 May 2012 - 01:08 PM

Without trying it out at this stage, I can immediately see a problem with your BubbleSprite. Firstly, it needs to subclass Pygame's sprite to register it as a subclass of a valid sprite:

class BubbleSprite(pygame.sprite.Sprite):
    def __init__(self, xpos, ypos, filename):


Secondly, you need to call the __init__ method in it's superclass to prepare the BubbleSprite for use as a Pygame sprite:
class BubbleSprite:
    def __init__(self, xpos, ypos, filename):
        # Call the superclass's initialization method
        pygame.sprite.Sprite.__init__(self)
        self.x = xpos
        self.y = ypos
        self.bitmap = pygame.image.load(filename)
        self.bitmap.set_colorkey((0,0,0))
        ....


Try both of them fixes and then post back if you have any more problems :)
Was This Post Helpful? 0
  • +
  • -

#3 Zephr  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 01-May 12

Re: [Pygame] 2D side scroller getting a sprite to shoot a simple bullet?

Posted 01 May 2012 - 01:46 PM

Ah that makes sense :)

import pygame
pygame.init()

screen = pygame.display.set_mode((1280, 700))

class BubbleSprite(pygame.sprite.Sprite):
    def __init__(self, xpos, ypos, filename):
        pygame.sprite.Sprite.__init__(self)
        self.x = xpos
        self.y = ypos
        self.image = pygame.image.load(filename)
        tranColor = self.image.get_at((0, 0))
        self.image.set_colorkey(tranColor)
        self.rect = self.image.get_rect() 
        
    def set_position(self, xpos, ypos):
        self.x = xpos
        self.y = ypos
    def render(self):
        screen.blit(self.image, (self.x, self.y))




That's my new class, no errors now however its still not working quite right. The image is just drawn in the very top left corner of the screen, it's not appearing once I press space, relative to my sprites location.
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: [Pygame] 2D side scroller getting a sprite to shoot a simple bullet?

Posted 01 May 2012 - 02:00 PM

Apologies for my slow replies, trying to do something else simultaneously. <_<

Can you clear up: is the sprite showing at the top left hand corner when you load the game or is it showing there only when you have pressed space?
Was This Post Helpful? 0
  • +
  • -

#5 Zephr  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 01-May 12

Re: [Pygame] 2D side scroller getting a sprite to shoot a simple bullet?

Posted 01 May 2012 - 02:08 PM

View PostSimown, on 01 May 2012 - 02:00 PM, said:

Apologies for my slow replies, trying to do something else simultaneously. <_<

Can you clear up: is the sprite showing at the top left hand corner when you load the game or is it showing there only when you have pressed space?



No problem with that mate. The bubble sprite is showing up on the screen when the game launches but is unresponsive to the space button.
Was This Post Helpful? 0
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: [Pygame] 2D side scroller getting a sprite to shoot a simple bullet?

Posted 01 May 2012 - 02:54 PM

I think I may have found the error:

In the BubbleSprite class file, you make a new screen and blit the sprite on to that. You never blit the sprite on to the main game's screen.

If you see here:
    def render(self):
        screen.blit(self.image, (self.x, self.y))

The BubbleSprite is blitted onto a local copy of the screen, rather than the screen in the main game.

Instead of render() doing what it does, you want something like this:
# in main.py
screen.blit(bubblebullet.image, (bubblebullet.x, bubblebullet.y))



I hope you see the difference between these two. One is using the main screen, your version was using a local copy of the screen you created:

#In the BubbleBullet class file - remove it!
screen = pygame.display.set_mode((1280, 700))

If there is no other solution put forward by tomorrow morning put forward, I'll find time to fiddle around with the code and hopefully it will become clear.
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: [Pygame] 2D side scroller getting a sprite to shoot a simple bullet?

Posted 01 May 2012 - 04:40 PM

Wow, I really showed up late to the party!

Well, if you don't mind me throwing my two cents in there, Game Programming can get really messy really fast. For that reason, I'd recommend utilizing the IDEA-ALTER framework proposed by Andy Harris in "Game Programming, The L Line, The Express Line to Learning". This framework creates an organizational structure to your main function.

IDEA represents setting up your game

I - import and initialize
D - Display
E - Entities
A - Action

ALTER is the action of your game

A - Assign Values
L - Loop (ie your main loop)
T - Timing
E - Events
R - Refresh Display

EDIT:
Upon taking a second look at your code, I noticed that you're already using IDEA ALTER! whoops :oops: Did you read that book too?

This post has been edited by atraub: 01 May 2012 - 04:56 PM

Was This Post Helpful? 0
  • +
  • -

#8 Zephr  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 01-May 12

Re: [Pygame] 2D side scroller getting a sprite to shoot a simple bullet?

Posted 04 May 2012 - 11:33 AM

View Postatraub, on 01 May 2012 - 04:40 PM, said:

Wow, I really showed up late to the party!

Well, if you don't mind me throwing my two cents in there, Game Programming can get really messy really fast. For that reason, I'd recommend utilizing the IDEA-ALTER framework proposed by Andy Harris in "Game Programming, The L Line, The Express Line to Learning". This framework creates an organizational structure to your main function.

IDEA represents setting up your game

I - import and initialize
D - Display
E - Entities
A - Action

ALTER is the action of your game

A - Assign Values
L - Loop (ie your main loop)
T - Timing
E - Events
R - Refresh Display

EDIT:
Upon taking a second look at your code, I noticed that you're already using IDEA ALTER! whoops :oops: Did you read that book too?




Yeah that's one of the books I got. :)

Was still trying with this over the weekend and still cant seem to get it to work. I tried changing the render method and just use a blit in the main but it hasn't changed.
Was This Post Helpful? 0
  • +
  • -

#9 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: [Pygame] 2D side scroller getting a sprite to shoot a simple bullet?

Posted 04 May 2012 - 03:13 PM

If you want, I can work with you more directly next week, once I get past the end of the semester. I've always enjoyed a little game development.
Was This Post Helpful? 0
  • +
  • -

#10 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: [Pygame] 2D side scroller getting a sprite to shoot a simple bullet?

Posted 04 May 2012 - 04:02 PM

Oooh, do you think you can post the updated pygame code blitting to the main screen. I am a bit of a self-confessed Pygame enthusiast, between us I'm sure we can figure it out.

This post has been edited by Simown: 04 May 2012 - 04:03 PM

Was This Post Helpful? 0
  • +
  • -

#11 Zephr  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 01-May 12

Re: [Pygame] 2D side scroller getting a sprite to shoot a simple bullet?

Posted 06 May 2012 - 01:44 PM

Hey, thanks for the offer atraub. If you would like to :)

Here's my updated main, I've gotten the bubble to appear when I press space now, just need it to travel -5 in y... (there might be a few commented lines added when trying out some things :) )


import pygame, motherWhale, scoreBoard, babyWhale, seaMonster, bubble
pygame.init()


def main():
    
    #global motherWhale
    #global seaWorld
    #global scoreBoard
    #global bubble
    
    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption("Bubble Battle")

    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((255, 255, 255))
    screen.blit(background, (0, 0))
    
    
    #pygame.mixer.music.load('')
    #pygame.mixer.music.play(-1, 0.0)
    #musicPlaying = True
    
    motherwhale = motherWhale.MotherWhale()
    babywhale = babyWhale.BabyWhale()
    monster1 = seaMonster.SeaMonster1()
    monster2 = seaMonster.SeaMonster2()
    bubblebullet = bubble.BubbleSprite(0, 480, 'bubble.png')
    #cloud3 = Cloud()
    #seaworld = seaWorld.SeaWorld()
    scoreboard = scoreBoard.Scoreboard()

    goodSprites = pygame.sprite.Group(motherwhale, babywhale, bubblebullet) #seaworld, island, plane)
    badSprites = pygame.sprite.Group(monster1, monster2)
    bulletSprite = pygame.sprite.Group(bubblebullet)
    #backroundSprite = pygame.sprite.Group(seaworld) 
    scoreSprite = pygame.sprite.Group(scoreboard)

    clock = pygame.time.Clock()
    keepGoing = True
    while keepGoing:
        clock.tick(30)
        
        
        
        #pygame.mouse.set_visible(False)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
                
                
                if bubblebullet.y < 479 and bubblebullet.y > 0:
                    bubblebullet.add(screen)
                    bubblebullet.x -= 5
                
                            
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE or event.key == pygame.K_q:
                    keepGoing = False
                    
                if event.key == pygame.K_UP:
                    motherwhale.y -= 25   
                    
                if event.key == pygame.K_DOWN:
                    motherwhale.y += 25
                    
                if event.key == pygame.K_SPACE:
                    bubblebullet.x = motherwhale.x
                    bubblebullet.y = motherwhale.y
                    
                #if event.key == pygame.K_SPACE:
                   # bubble.x = motherwhale.x
                    #bubble.y = motherwhale.y
                    
                #if event.key == pygame.K_RIGHT:
                
            
                    #motherWhale.x += 20
                    
                #if event.key == pygame.K_LEFT:
                    #motherWhale.x -= 20
                    
                        
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_UP:                    #WHEN KEYPRESS LIFTED, STOP SPRITE
                    motherwhale.speed = 0
                    
                elif event.key == pygame.K_DOWN:
                    motherwhale.speed = 0
                               
                    
                if motherwhale.y >= screen.get_width():
                    motherWhale.speed = 0
                    
                if motherwhale.y >= screen.get_height():
                    motherWhale.speed = 0
                    '********************E'
                    
                    
                #if bubble.y < 479 and bubble.y > 0:
                    #bubble.render()
                    #bubble.y -= 5
                    
                    
                    
        #TODO: change to sprite.collideany
        
        #check collisions
        
        if motherwhale.rect.colliderect(babywhale.rect):
            #plane.sndYay.play()
            babywhale.reset()
            scoreboard.score += 1000

        collideMonster = pygame.sprite.spritecollide(motherwhale, badSprites, False)
        if collideMonster:
            #plane.sndThunder.play()
            scoreboard.lives -= 1
            if scoreboard.lives <= 0:
                keepGoing = False
            for monster in collideMonster:
                monster.reset()
        
        
        screen.blit(bubblebullet.image, (bubblebullet.x, bubblebullet.y))

        goodSprites.clear(screen, background)
        bulletSprite.clear(screen, background)
        #backroundSprite.clear(screen, background)
        badSprites.clear(screen, background)
        scoreSprite.clear(screen, background)
                    
        goodSprites.update()
        bulletSprite.update()
        #backroundSprite.update()
        badSprites.update()
        scoreSprite.update()
        
        goodSprites.draw(screen)
        bulletSprite.draw(screen)
        #backroundSprite.draw(screen)
        badSprites.draw(screen)
        scoreSprite.draw(screen)
        
        pygame.display.flip()
    
    #return mouse cursor
    #pygame.mouse.set_visible(True)
    #motherwhale.sndEngine.stop()

 

    
if __name__ == "__main__":
    main()



*Excuse me I meant x not y.
Was This Post Helpful? 0
  • +
  • -

#12 Zephr  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 01-May 12

Re: [Pygame] 2D side scroller getting a sprite to shoot a simple bullet?

Posted 07 May 2012 - 10:47 AM

Also, the render() from the bullet class is the only thing that I have removed. So that class is pretty much the same.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1