I'm trying to create a gun in a game i am making but having trouble. I cant figure out how to append a rect into the while loop when i click something like a space bar. It works in javascript but the body.append() function doesn't exist in python so how am i suppose to do it?
thanks in advance
EDIT:
I figured it out.
import pygame
import sys
class Application(object):
def __init__(self,screen=None):
self.screen = screen
self.fps = 30
self.clock = pygame.time.Clock()
self.buffer()
def buffer(self):
self.char = Character(self.screen)
self.gun = Gun(self.screen)
while 1:
self.key = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
return 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if len(self.gun.ammo) <= 10 and self.gun.magazine_count > 0:
self.gun.ammo.append(self.gun.bullet_position(self.char.x+20,self.char.y+10))
elif len(self.gun.ammo) > 10:
self.gun.magazine_count -= 1
if self.gun.magazine_count > 0:
print("reloading")
self.gun.ammo = []
else:
print("out of magazines < press UP arrow to restock >")
if event.key == pygame.K_UP:
if self.gun.magazine_count == 0:
self.gun.magazine_count = 4
self.screen.fill((0,0,0))
self.char.draw_char()
for x in range(len(self.gun.ammo)):
self.gun.bullet_create(self.gun.ammo[x],5)
self.gun.ammo[x][0] += 25
pygame.display.update()
self.clock.tick(self.fps)
pygame.quit()
sys.exit()
quit()
class Character(object):
def __init__(self,screen):
self.screen = screen
self.w, self.h = 20, 20
self.x, self.y = 0, int(self.screen.get_height()/2-self.h)
def draw_char(self):
player = pygame.draw.rect(self.screen,(255,0,0),[self.x,self.y,self.w,self.h])
class Gun(object):
def __init__(self,screen):
self.screen = screen
self.magazine_count = 4
self.ammo = []
def bullet_position(self,x,y):
return [x,y]
def bullet_create(self,dimensions,radius):
pygame.draw.circle(self.screen,(255,255,255),(dimensions),radius)
def Main():
pygame.init()
screen = pygame.display.set_mode((500,400))
app = Application(screen)
if __name__ == "__main__":
Main()
You should use an list of render objects. Everytime you render you go through that list and call the objects render method. To provide this you could use an interface every renderable object has to extends providing the render method.
As for your problem you simply add the new object you want to be drawn to the list and it automatically renders it at the next loop iteration.
Be sure that you never add something to a list while it beeing accessed when multithreading through!
*Edit: As requested - an example code:
class IRenderable:
def render(self, <other parameters>): raise NotImplementedError
Here you can of course add parameters needed for rendering. I don't know how this is handled in Pyhton but in Java you would pass an Graphics object containing all the rendering methods for example. Every renderable class now is supposed to extend this interface.
class Gun(IRenderable):
def render(self):
# enter your rendercode here
In your main you just have to have an list of those objects called renderables and iterate through them to render with the following method:
def render(self):
for renderable in self.renderables:
renderable.render()
Of course you would pass your parameters here. Whenever you want to render you call this method. (e.g. in your main-loop as you stated in your question.)