2

I'm making a submarine game in pygame and I tried to make my submarine shoot bullets. I've tried the code, that I used in my other pygame project, however it doesn't work in this one. Compiler doesn't give any error, but when I press space, it won't shoot. I tried to find a mistake, but I couldn't. I also tried to browse Stack Overflow, but didn't find the answer I was looking for. Here's the code:

import pygame

pygame.init()
run = True
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption('Podmornca')
desno = pygame.image.load('podmornicaD.png')
levo = pygame.image.load('podmornica.png')
ozadje = pygame.image.load('ozadje.png')
torpedoD = pygame.image.load('torpedo.png')
torpedoL = pygame.image.load('torpedoL.png')
class podmornica():
    def __init__(self, x, y, v):
        self.x = x
        self.y = y
        self.v = v
        self.ziv = 100
        self.levo = False
        self.desno = True
    def naris(self):
        if self.levo:
            screen.blit(levo, (self.x, self.y))
        elif self.desno:
            screen.blit(desno, (self.x, self.y))
class torpedo():
    def __init__(self, x, y, smer):
        self.x = x
        self.y = y 
        self.smer = smer
        self.v = 5 * smer    
    def naris(self, screen):
        if self.smer < 0:
            screen.blit(torpedoD, (self.x, self.y))
        else:
            screen.blit(torpedoL, (self.x, self.y))     
igralec = podmornica(150, 300, 10)
#the list of bullets:
metki = []
def grafika():
    screen.blit(ozadje, (0,0))
    igralec.naris()
    #Here is code for displaying the bullets
    for metek in metki:
        metek.naris(screen)
    pygame.display.flip()
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    #This piece of code is for moving of bullets:
    for metek in metki:
        if metek.x < 600 and metek.x > 0:
            metek.x +=  metek.v
        else:
            metki.pop(metki.index(metek))        
    if event.type == pygame.KEYDOWN and event.key == pygame.K_UP and igralec.y > 10:
        igralec.y -= 3
    if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN and igralec.y < 350:
        igralec.y += 3
    if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
        igralec.levo = True
        igralec.desno = False
    if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
        igralec.levo = False
        igralec.desno = True
    #the trigger for bullet:
    if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
        if igralec.levo:
            smer = -1
        else:
            smer = 1
        if len(metki) < 5:
            metki.append(torpedo(igralec.x, igralec.y, smer))
    grafika()
pygame.quit()

1 Answers1

2

The events have to be handled in the event loop. If you want to achieve a smooth movement, then you have get the key states by pygame.key.get_pressed(). Furthermore the bullets move much to fast. Control the frames by pygame.time.Clock() respectively .tick(). e.g:

FPS = 60
clock = pygame.time.Clock()   
while run:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.KEYDOWN:
            #the trigger for bullet:
            if event.key == pygame.K_SPACE:
                if igralec.levo:
                    smer = -1
                else:
                    smer = 1
                if len(metki) < 5:
                    metki.append(torpedo(igralec.x, igralec.y, smer))

    # This piece of code is for moving of bullets:
    for metek in metki[:]:
        if metek.x < 600 and metek.x > 0:
            metek.x += metek.v
        else:
            metki.remove(metek) 

    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP] and igralec.y > 10:
        igralec.y -= 3
    if keys[pygame.K_DOWN] and igralec.y < 350:
        igralec.y += 3
    if keys[pygame.K_LEFT]:
        igralec.levo = True
        igralec.desno = False
    if keys[pygame.K_RIGHT]:
        igralec.levo = False
        igralec.desno = True  
    grafika()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174