Bullet sprite creation for 2D game
Page 1 of 113 Replies - 1721 Views - Last Post: 27 July 2012 - 01:24 AM
#1
Bullet sprite creation for 2D game
Posted 18 July 2012 - 07:43 AM
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.
Replies To: Bullet sprite creation for 2D game
#2
Re: Bullet sprite creation for 2D game
Posted 18 July 2012 - 09:09 AM
This post has been edited by atraub: 18 July 2012 - 09:09 AM
#3
Re: Bullet sprite creation for 2D game
Posted 18 July 2012 - 10:14 AM
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)
#4
Re: Bullet sprite creation for 2D game
Posted 18 July 2012 - 10:44 AM
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
#5
Re: Bullet sprite creation for 2D game
Posted 19 July 2012 - 03:26 AM
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()
#6
Re: Bullet sprite creation for 2D game
Posted 19 July 2012 - 07:15 AM
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
#7
Re: Bullet sprite creation for 2D game
Posted 19 July 2012 - 10:05 AM
Thanks.
#8
Re: Bullet sprite creation for 2D game
Posted 19 July 2012 - 05:14 PM
#9
Re: Bullet sprite creation for 2D game
Posted 22 July 2012 - 01:54 PM
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.
#10
Re: Bullet sprite creation for 2D game
Posted 22 July 2012 - 03:51 PM
#11
Re: Bullet sprite creation for 2D game
Posted 22 July 2012 - 07:23 PM
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
#12
Re: Bullet sprite creation for 2D game
Posted 24 July 2012 - 12:07 AM
Simown, on 23 July 2012 - 07:53 AM, said:
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.
#13
Re: Bullet sprite creation for 2D game
Posted 24 July 2012 - 09:01 AM
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.
#14
Re: Bullet sprite creation for 2D game
Posted 27 July 2012 - 01:24 AM
Simown, on 24 July 2012 - 09:31 PM, said:
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.
|
|

New Topic/Question
Reply



MultiQuote





|