1
import pygame
# initialize the pygame
pygame.init()

# create the screen
screen = pygame.display.set_mode((800, 600))

# Title and icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('Invader2.xcf')
pygame.display.set_icon(icon)

# Player
playerImg = pygame.image.load('Player.xcf')
player_big = pygame.transform.scale(playerImg, (48, 48))
playerX = 370
playerY = 480
vel = 0.15

# Player Bullets
playerBulletImg = pygame.image.load('Player_bullet.xcf')
playerBulletX = playerX + 16
playerBulletY = playerY + 10
bulletVel = 0.3


def player(x, y):
    screen.blit(player_big, (playerX, playerY))


def bullet(x, y):
    screen.blit(playerBulletImg, (playerBulletX, playerBulletY))


# Game Loop
running = True


while running:

    # RGB
    screen.fill((18.8, 83.5, 78.4))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and playerX > 0:
        playerX -= vel
        playerBulletX -= vel
    if keys[pygame.K_RIGHT] and playerX < 800 - 48:
        playerX += vel
        playerBulletX += vel
    if keys[pygame.K_SPACE]:
        playerBulletY -= 0.5
    if playerBulletY == 0:
        playerBulletY = 480
    bullet(playerBulletX, playerBulletY)
    player(playerX, playerY)

    pygame.display.update()

Hi! I'm a beginner who has just started using Pygame. I'm making a Space Invaders / Galaga type game. When I press the space bar, the bullet goes up the y-axis, but only when I hold it down. I want the bullet to continue to move up the screen, even when I'm not holding down the space bar. Also, another problem is that when the bullet is in the air it still follows the x-value for the player. So when the bullet shoots up, It moves around depending on where the player is.

2 Answers2

1

you should run your game as a class it is much more easier to code pygame with classes you can change your b.move to where else

import pygame
# initialize the pygame
pygame.init()

# create the screen
screen = pygame.display.set_mode((800, 600))

# Title and icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('Invader2.xcf')
pygame.display.set_icon(icon)

# Player

class Player:
    def __init__(self):
        self.Img = pygame.image.load('Player.xcf')
        self.big = pygame.transform.scale(playerImg, (48, 48))
        self.x = 370
        self.y = 480
        self.vel = 0.15
    
    def draw(self,screen):
        screen.blit(self.big, (self.x, self.y))


class Bullet:
    def __init__(self):
        self.Img = pygame.image.load('Player_bullet.xcf')
        self.x = playerX + 16
        self.y = playerY + 10
        self.vel = 0.3

    def draw(screen):
        screen.blit(self.Img, (self.y, self.y))

    def move():
        self.y -= self.vel

def draw(p,b):
    p.draw()
    for i in b:
        i.draw()

# Game Loop


def main():
    running = True
    p = Player()
    b = []
    while running:
        screen.fill((18.8, 83.5, 78.4))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        keys = pygame.key.get_pressed()

        if keys[pygame.K_LEFT] and p.x > 0:
            p.x -= p.vel
        if keys[pygame.K_RIGHT] and p.x < 800 - 48:
            p.x += p.vel
        if keys[pygame.K_SPACE]:
           b.append(Bullet())
        draw(p,b)
        for bullet in b:
             bullet.move()

        pygame.display.update()
1

Use the keyboard event instead of pygame.key.get_pressed() to shoot a bullet.

pygame.key.get_pressed() returns a sequence with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement.
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action.

Add a shootBullet variable that indicates when the bullet will be fired. Set the state when SPACE is pressed. Move and draw the bullet depending on this state. Set the starting position of the bullet when the bullet is fired.

shootBullet = False

clock = pygame.time.Clock()
running = True
while running:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                shootBullet = True
                playerBulletX = playerX 
                playerBulletY = 480

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and playerX > 0:
        playerX -= vel
    if keys[pygame.K_RIGHT] and playerX < 800 - 48:
        playerX += vel

    if shootBullet:
        playerBulletY -= 1
        if playerBulletY <= 0:
            shoot_bullet = False
        
    screen.fill((18.8, 83.5, 78.4))
    if  shootBullet:   
        bullet(playerBulletX, playerBulletY)
    player(playerX, playerY)
    pygame.display.update()

Use pygame.time.Clock to control the frames per second and thus the game speed.

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick():

This method should be called once per frame.

That means that the loop:

clock = pygame.time.Clock()
run = True
while run:
   clock.tick(100)

runs 100 times per second.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174