13 Replies - 3826 Views - Last Post: 27 July 2012 - 01:24 AM Rate Topic: -----

#1 jephindavis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 13-November 09

Bullet sprite creation for 2D game

Posted 18 July 2012 - 07:43 AM

Hi, I have been working on a 2D game using pygame library and i
tried to create a bullet sprite. The bullet should appear when i press
the space bar and continue to move forward until it hits the target or
the window frame. The problem is that i managed to display the bullet when pressing space,
but it does not move forward. Any help will be appreciated. I will post the code if asked for.
Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: Bullet sprite creation for 2D game

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Bullet sprite creation for 2D game

Posted 18 July 2012 - 09:09 AM

Just assume the code has been asked for when you post. It'll save time for all of us.

This post has been edited by atraub: 18 July 2012 - 09:09 AM

Was This Post Helpful? 0
  • +
  • -

#3 jephindavis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 13-November 09

Re: Bullet sprite creation for 2D game

Posted 18 July 2012 - 10:14 AM

here is my bullet class
class Bullet(pygame.sprite.Sprite):
      def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image=pygame.image.load("bullet.png")
            self.rect=self.image.get_rect()
            self.rect.x=100
            self.rect.y=50
            #screen.blit(self.image,(self.rect.x,self.rect.y))
      def update(self):
            self.rect.x+=10




and i have used a list of objects as
bullet=[]



now i wonder whether i should create a sprite group for the bullet, and also when should i give the update ?

the code i used for appending bullet object to the list when i press space is

for event in pygame.event.get():
                
              if event.type==pygame.KEYDOWN and event.key==pygame.K_f:
                    bult=Bullet()
                    bulletsprite=pygame.sprite.Group(bult)
                    bullet.append(bult)
                    bulletsprite.update()
                    bulletsprite.draw(screen)


Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Bullet sprite creation for 2D game

Posted 18 July 2012 - 10:44 AM

The IDEA/ALTER framework was introduced by a gentleman named Andy Harris (as far as I know). The framework goes like this:

I – Import and initialize
D – Display configuration
E – Entities
A – Action (broken into ALTER steps)

A – Assign values to key variables
L – Loop (setup main Loop)
T – Timer to set frame rate
E – Event handling
R - Refresh the display

The update method would probably constitute as event handling, so I would take care of it in the event handling phase. However, this does not mean to update all the sprites in the loop that you posted above. If you post more code I can be more specific. Does this help?

This post has been edited by atraub: 18 July 2012 - 11:20 AM

Was This Post Helpful? 1
  • +
  • -

#5 jephindavis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 13-November 09

Re: Bullet sprite creation for 2D game

Posted 19 July 2012 - 03:26 AM

actually, i started studying pygame on my own, so i haven't gone through the above said method of programming. The problem I'm facing is that the bullet does appear when i press space but it won't move forward, even though i have added the update function when the event occurs. Should i add the update outside the event handler? I can post the full code if you want, but its so messy, because i didn't use sprite sheet for my character. The edited code is:
import pygame
from pygame import *

pygame.init()
pygame.key.set_repeat(1, 1)
screen = pygame.display.set_mode((0,0),FULLSCREEN)


            
            
class GhostShadow(pygame.sprite.Sprite):
      def __init__(self):
          pygame.sprite.Sprite.__init__(self)
          self.image=pygame.image.load("ghost shadow/Frame0.png")
          self.rect=self.image.get_rect()
          self.rect.x=620
          self.rect.y=510
          

      def update(self):
            self.rect.x-=6
      def reset(self):
            self.rect.x=620
     
class BG(pygame.sprite.Sprite):
      def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image=pygame.image.load("BG.jpg")
            self.rect=self.image.get_rect()
            self.rect.y=120
            
      def update(self):
            
            if self.rect.right==1282:
                  self.rect.right=3844
            self.rect.right-=6
            
      
class Bullet(pygame.sprite.Sprite):
      def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image=pygame.image.load("bullet.png")
            self.rect=self.image.get_rect()
            self.rect.x=100
            self.rect.y=50
            
      def update(self):
            self.rect.x+=10
      


     

class Shadow(pygame.sprite.Sprite):
      def __init__(self):
          pygame.sprite.Sprite.__init__(self)
          self.image=pygame.image.load("frames/Stand1.png")
          self.rect=self.image.get_rect()
          self.rect.x=20
          self.rect.y=510

      def update(self):
          self.image=pygame.image.load("frames/Frame0.png")
          self.rect=self.image.get_rect()
          self.rect.x=20
          self.rect.y=510

      



     
def main():
      
      
      background = pygame.Surface(screen.get_size())
      background=pygame.image.load("backclothwithclose.jpg")
      close=pygame.image.load("backclothwithcloseup.jpg")
      
      screen.blit(background,(0,0))
      clock =pygame.time.Clock()
     
     
      
      
      shadow = Shadow()
      
      ghost=GhostShadow()
      bg=BG()
     
      bullet=[]
      
      bgsprite=pygame.sprite.Group(bg)
      allSprites = pygame.sprite.Group(shadow)
      
      ghostsprite=pygame.sprite.Group(ghost)     
      bulletsprite=pygame.sprite.Group(bullet)
      
      keepGoing = True
      while keepGoing:
           
          
          x,y=pygame.mouse.get_pos()
          if x>1052 and x<1166 and y>35 and y<64:
                screen.blit(close,(0,0))
          else:
                screen.blit(background,(0,0))
                
          for event in pygame.event.get():
                
              if event.type==pygame.KEYDOWN and event.key==pygame.K_f:
                    bult=Bullet()
                    bulletsprite=pygame.sprite.Group(bult)
                    bullet.append(bult)
                    bulletsprite.update()
                    bulletsprite.draw(screen)
              if event.type == pygame.QUIT or(event.type==pygame.KEYDOWN and event.key==pygame.K_ESCAPE): 
                    keepGoing = False
                     
                    
               
              else:
                    bgsprite.clear(screen,background)
                    
                    bgsprite.draw(screen)
                    
                    shadow.update()
                    ghostsprite.draw(screen)
                    
                    allSprites.draw(screen)
                    
                    pygame.display.flip()
                    
              bulletsprite.update()
              bulletsprite.draw(background)
              
              simplecollide=pygame.sprite.collide_rect(ghost,shadow)
           
              if simplecollide:
                    
                    ghost.reset()
                    
             
                    
                    
          
            
          
          
if __name__ == "__main__":
      main()


Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Bullet sprite creation for 2D game

Posted 19 July 2012 - 07:15 AM

you need to move the update method outside of the event loop. This loop: for event in pygame.event.get(): should only handle processing the event objects, there's no reason for it to handle blitting or updating sprites.

I'd really recommend reading about IDEA/ALTER to better organize your code. Here's a quick and dirty example I found of it:
#Import and initialize
import pygame
pygame.init()
#Display
scr=pygame.display.set_mode((320,240))
pygame.display.set_caption("Alt-F4 = QUIT")
#Entities
bg=pygame.Surface(scr.get_size()).convert()
bg.fill((250,150,50))
fnt=pygame.font.SysFont("None",24)
lbl=fnt.render("IDEA-ALTER",1,(255,255,0))
#Action -> ALTER
################
#Assign values
clock=pygame.time.Clock()
keepGoing = True
#Loop
while keepGoing:
#Timing
    clock.tick(30)
#Events
    for evnt in pygame.event.get():
        if evnt.type==pygame.QUIT:
            keepGoing = False
    #update all your sprites here
    #for sprite in spriteList:
        #sprite.update()
#Refresh display
    scr.blit(bg,(0,0))
    scr.blit(lbl,(100,100))
    pygame.display.flip()



EDIT:
I should point out that I haven't used pygame in years, so I'm working from fuzzy memories.

EDIT 2:
This should be profoundly helpful. It's Andy Harris's book, I read it years ago and I enjoyed it very much.

This post has been edited by atraub: 19 July 2012 - 09:40 AM

Was This Post Helpful? 1
  • +
  • -

#7 jephindavis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 13-November 09

Re: Bullet sprite creation for 2D game

Posted 19 July 2012 - 10:05 AM

well, i still have some more doubts. Let me refer the book you recommended, and will get back to you.
Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Bullet sprite creation for 2D game

Posted 19 July 2012 - 05:14 PM

You did notice that I made a comment that if you move the sprite update outside the for loop, it should work, right?
Was This Post Helpful? 0
  • +
  • -

#9 jephindavis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 13-November 09

Re: Bullet sprite creation for 2D game

Posted 22 July 2012 - 01:54 PM

yes it works.. thank you..
Now, i am having another doubt, it might sound really dumb, but..,
if i am having an image which is a sprite that moves continuously, is there a method for me to 'blit' an image over the sprite surface?
That is, i have a class
class BG(pygame.sprite.Sprite):
      def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image=pygame.image.load("BG.jpg")
            self.rect=self.image.get_rect()
            self.rect.y=120
            
      def update(self):
            
            if self.rect.right==1282:
                  self.rect.right=3844
            self.rect.right-=6



so the image will be moving,if i create an object of this class and do the updates and draw functions properly, right?
what i want to do is blit an image over this by using <surface>.blit method.
But i am not able to keep the sprite surface as the surface, so the blitting only occurs below the sprite. I know this is stupid, bt i kinda found myself stuck at this.. So any help is appreciated. Thanks.
Was This Post Helpful? 0
  • +
  • -

#10 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Bullet sprite creation for 2D game

Posted 22 July 2012 - 03:51 PM

I'm sorry, it's been so long since I've used pygame that I don't completely follow. I need you to dumb it down a little for me :)
Was This Post Helpful? 0
  • +
  • -

#11 Simown  Icon User is offline

  • Blue Sprat
  • member icon

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

Re: Bullet sprite creation for 2D game

Posted 22 July 2012 - 07:23 PM

Maybe we need a little more information but, have you considered the order of your statements?

If you have a blank screen then blit something on it, then blit the sprite on, the sprite will be "on the top"

If you have the same screen, and blit the sprite first, then something else, that second blit will be "on the top"

This is assuming you are blitting to the same surface.

This post has been edited by Simown: 22 July 2012 - 07:24 PM

Was This Post Helpful? 0
  • +
  • -

#12 jephindavis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 13-November 09

Re: Bullet sprite creation for 2D game

Posted 24 July 2012 - 12:07 AM

View PostSimown, on 23 July 2012 - 07:53 AM, said:

Maybe we need a little more information but, have you considered the order of your statements?

If you have a blank screen then blit something on it, then blit the sprite on, the sprite will be "on the top"

If you have the same screen, and blit the sprite first, then something else, that second blit will be "on the top"

This is assuming you are blitting to the same surface.



Actually i don't know how to blit over the sprite surface.
For blitting over the screen, i used screen.blit(..), where screen is my surface, but for blitting over the sprite image, i don't know what should i use as surface.
Was This Post Helpful? 0
  • +
  • -

#13 Simown  Icon User is offline

  • Blue Sprat
  • member icon

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

Re: Bullet sprite creation for 2D game

Posted 24 July 2012 - 09:01 AM

I think you are a bit confused at what an "image" is when you load it in pygame.

When you do self.image = pygame.image.load("image.png"), self.image is actually a surface now which contains the image. It it not just a png or a jpg now.

To blit on top of this sprite you'd do self.image.blit(...) just like you'd blit on to the screen.
Was This Post Helpful? 1
  • +
  • -

#14 jephindavis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 13-November 09

Re: Bullet sprite creation for 2D game

Posted 27 July 2012 - 01:24 AM

View PostSimown, on 24 July 2012 - 09:31 PM, said:

I think you are a bit confused at what an "image" is when you load it in pygame.

When you do self.image = pygame.image.load("image.png"), self.image is actually a surface now which contains the image. It it not just a png or a jpg now.

To blit on top of this sprite you'd do self.image.blit(...) just like you'd blit on to the screen.




well, i know that, but i was confused, since it was a sprite class and i was using a spritegroup name to update, so i thought i could use the same for blitting, which didn't work out. Well i had it figured out. Thanks for the reply.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1