2

code:

import pygame
import sys
from pygame.locals import *

count = 0
moving = False

def blast1():
    blast.y -= bl

def player_animation():
    global player_speed
    player.x = player_speed

pygame.init()
running = True

clock = pygame.time.Clock()

bl = 1
player_speed = 1

player = pygame.Rect(250, 450, 50, 50)
blast = pygame.Rect(player.x, player.y , 10, 10)

screen = pygame.display.set_mode((500, 500))
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    player_animation()
    blast1()

    screen.fill((255, 255, 255)) #color
    pygame.draw.ellipse(screen, [0, 0, 255], player)
    pygame.draw.ellipse(screen, [0, 255, 0], blast)
       
    keys=pygame.key.get_pressed()
    if keys[K_LEFT] and player.x !=  -4 :
        player_speed -= 5

    if keys[K_RIGHT] and player.x !=  451:
        player_speed += 5
    pygame.display.flip()
    
    clock.tick(30)
blast.x = player.x
pygame.quit()

My objective is pretty simple. I want to create a ball that you can move to the right and left. Then I want to create a bullet that is fired from the position of the "player". I'm having some trouble with this, I don't know why but when I declare the position of the bullet blast = pygame.Rect(player.x, player.y , 10, 10) only the y position works. The x position is somehow in the middle of the screen instead of on the player. How can I fix this?

red_panda
  • 364
  • 2
  • 18
  • did You forget to indent `blast.x = player.x`? it only assigns when out of loop, basically when You exit the game – Matiiss Apr 27 '21 at 11:13

1 Answers1

1

The line

blast = pygame.Rect(player.x, player.y , 10, 10)

assigns a Rect to blast, with the position of the player at this moment. This blast will stay still until you edit its position. You only change the y position here:

blast.y -= bl

But you don't update the x position when you launch the bullet. You could do this like that:

def launch_blast(): # call this function when you create the bullet.
                    # At this point in the program, just call it at the beginning.
    blast.x = player.x + player.width / 2 - blast.width / 2

def blast1():
    blast.y -= bl

To launch several bullets, you can use this code:

import pygame
from pygame.locals import *
pygame.init()

class Bullet:
    def __init__(self):
        self.rect = Rect(player.rect.x - 5 + player.rect.width / 2, player.rect.y , 10, 10)

    def move(self):
        self.rect.y -= 10 # move up
        pygame.draw.ellipse(screen, [0, 255, 0], self.rect) # draw on screen

class Player:
    def __init__(self):
        self.rect = pygame.Rect(250, 450, 50, 50)

    def move(self):
        # move
        keys = pygame.key.get_pressed()
        if keys[K_LEFT]:
            self.rect.x -= 5
        if keys[K_RIGHT]:
            self.rect.x += 5

        # don't go out of the screen
        self.rect.x = min(max(self.rect.x, 0), 500 - self.rect.width)

        # draw on screen
        pygame.draw.ellipse(screen, [0, 0, 255], self.rect)

player = Player()
bullets = []

clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 500))
running = True

while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
        if event.type == KEYDOWN and event.key == K_SPACE:
            bullets.append(Bullet()) # generate a new bullet when space pressed

    screen.fill((255, 255, 255)) # white

    for bullet in bullets: # draw each bullet generated
        bullet.move()
        if bullet.rect.x < bullet.rect.height: # if out of the screen,
            bullets.remove(bullet) # then remove it
    player.move() # draw the player

    pygame.display.flip()
    clock.tick(30)
D_00
  • 1,440
  • 2
  • 13
  • 32