Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep shooting as long as the shoot button is being held down

Tags:

I recently worked a fire rate into my coding, however, I have to continuously press the fire button in order to shoot.

Is there a way to handle KEYDOWN events so that instead of firing one bullet when I press the fire button, bullets fire periodically so long as I'm holding down the fire button?

Working code is as follows;

Main game module

import pygame
from constants import *
from player import Player
from Projectile import Projectile
from pygame.math import Vector2

pygame.init()

screen = pygame.display.set_mode([500, 500])

pygame.display.set_caption('Labyrinth')

# Spawn player

player = Player(50, 50)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(player)

projectiles = pygame.sprite.Group()

clock = pygame.time.Clock()
previous_time = pygame.time.get_ticks()
speed = 12


done = False


# ----- Event Loop

while not done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        elif event.type == pygame.KEYDOWN:

            if event.key == pygame.K_LEFT:
                vel = Vector2(-speed, 0)
            elif event.key == pygame.K_RIGHT:
                vel = Vector2(speed, 0)
            elif event.key == pygame.K_UP:
                vel = Vector2(0, -speed)
            elif event.key == pygame.K_DOWN:
                vel = Vector2(0, speed)


            if event.key in (pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN):
                current_time = pygame.time.get_ticks()

                if current_time - previous_time > 500:
                    previous_time = current_time
                    projectiles.add(Projectile(player.rect.x, player.rect.y, vel))


# ----- Game Logic

    all_sprites_list.update()
    projectiles.update()



    screen.fill(GREEN)

    all_sprites_list.draw(screen)
    projectiles.draw(screen)


    pygame.display.flip()

    clock.tick(60)

pygame.quit()

Player module

from constants import *
import pygame
import time
from datetime import datetime, timedelta

class Player(pygame.sprite.Sprite):


    def __init__(self, x, y):

        super().__init__()

        self.image = pygame.Surface([15, 15])
        self.image.fill(BLACK)

        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.fire_rate = 1

        self.change_x = 0
        self.change_y = 0

    def changespeed(self, x, y):
        self.change_x += x
        self.change_y += y

    def update(self):
        self.rect.x += self.change_x
        self.rect.y += self.change_y

Projectile module

import pygame
from constants import *
from pygame.math import Vector2

BULLET_IMG = pygame.Surface((4, 4))
BULLET_IMG.fill(RED)

class Projectile(pygame.sprite.Sprite):

    def __init__(self, x, y, vel):

        super().__init__()

        self.image = BULLET_IMG
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.vel = Vector2(vel)


    def update(self):

        self.rect.move_ip(self.vel)

Any help would be greatly appreciated!

like image 441
Goose _ Avatar asked Aug 24 '18 14:08

Goose _


1 Answers

If you're not interested in the fact that a key is pressed, but rather that a key is held down, use pygame.key.get_pressed to get the keyboard state.

One way to solve your issue is to create a dict that maps each arrow key to a vector, and then simply iterate that dict and check if the specific key is pressed, something like this:

keymap = {
    pygame.K_LEFT:  Vector2(-speed, 0),
    pygame.K_RIGHT: Vector2(speed, 0),
    pygame.K_UP:    Vector2(0, -speed),
    pygame.K_DOWN:  Vector2(0, speed)
}

while not done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    current_time = pygame.time.get_ticks()

    pressed = pygame.key.get_pressed()
    for key in keymap:
        if pressed[key]:
            if current_time - previous_time > 500:
                previous_time = current_time
                projectiles.add(Projectile(player.rect.x, player.rect.y, keymap[key]))

Some further notes:

You can add sprites to multiple groups, and it usually makes sense to add all sprites to a groups that holds all sprites to keep the main loop simple.

You already have a group called all_sprites_list, so use it like this. Just add all new projectiles to that groups, also, and call update/draw only on that very group:

...
                if current_time - previous_time > 500:
                    previous_time = current_time
                    Projectile(player.rect.x, player.rect.y, keymap[key], all_sprites_list, projectiles)


# ----- Game Logic
    all_sprites_list.update()
    screen.fill(GREEN)
    all_sprites_list.draw(screen)
    pygame.display.flip()

...

and in Projectile.py:

...
class Projectile(pygame.sprite.Sprite):

    def __init__(self, x, y, vel, *groups):

        super().__init__(groups)
...
like image 76
sloth Avatar answered Sep 28 '22 17:09

sloth