Oscar
Oscar

Reputation: 107

Make object shoot - Pygame

Im new to python and for my course at university we have been set the task of creating a game using pygame, I obtained help from some Youtube tutorials and this is the code I have so far:

import pygame, sys
from pygame.locals import *

pygame.init()

window = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
black = (0,0,0)
white = (255,255,255)
background = pygame.image.load('grassy.gif')
turret = pygame.image.load('turret.png').convert_alpha()
pygame.display.set_caption("Tank Blast!")
moveX, moveY = 0,0

class Turret:

 def __init__ (self, x, y):

    self.x=x
    self.y=y
    self.width=50
    self.height=50
    self.i0 = pygame.image.load("turret.png")
    self.i1 = pygame.image.load("turret2.png")
    self.timeTarget = 10
    self.timeNum = 0
    self.currentImage = 0

def update (self):

    self.timeNum+=1

    if (self.timeNum==self.timeTarget):

        if (self.currentImage==0):

            self.currentImage+=1

        else:

            self.currentImage=0

        self.timeNum=0

    self.render()

def render(self):

    if (self.currentImage==0):

        window.blit(self.i0, (self.x, self.y))

    else:

        window.blit(self.i1, (self.x, self.y))

player = Turret(380, 480)
while True:
for event in pygame.event.get():

    if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):

        sys.exit()

    if (event.type==pygame.KEYDOWN):

        if (event.key==pygame.K_LEFT):

            moveX = -7

        if (event.key==pygame.K_RIGHT):

            moveX = 7

    if (event.type==pygame.KEYUP):

        if (event.key==pygame.K_LEFT):

            moveX = 0

        if (event.key==pygame.K_RIGHT):

            moveX = 0


window.blit(background, (0,0))

player.x+=moveX

player.y+=moveY

player.update()

pygame.display.flip()

clock.tick(60)

pygame.quit()

My question is, what would I have to do to make the turret shoot bullets by pressing the space bar? If anyone can help me Id appreciate it a lot,

Thank you

Upvotes: 0

Views: 1706

Answers (1)

sshashank124
sshashank124

Reputation: 32189

You should create a Bullet class. When the Turret shoots it creates a new Bullet object that will probably have a method to move and another method like destroy where it will check whether it is colliding with something and then destroy itself and that object. Also, your Bullet class should inherit from pygame.sprite.Sprite as follows:

class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        ...
    ...

That way when you create a lot of bullets you can group them together into pygame.sprite.Group. For example, if you create a pygame.sprite.Group called bullets to which you added all your bullet instances you can then update all the bullets by calling:

bullets.update() #this will then call the update() method of the individual bullets

I suggest you read about pygame.sprite.Sprite and pygame.sprite.Group.

They are really handy when dealing with multiple instances of similar objects.

Note: I'd recommend using pygame.display.update() instead of pygame.display.flip(). It is a better way of doing it.

Happy Pygaming!

Upvotes: 2

Related Questions