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?