1

So I wrote my code and everything works how I want it to for now. The only problem is that my bullet or fireball teleports to the location I set it to. I am wondering if there is a way to make it look like its actually moving towards the destination using pygame and whatnot. Thanks a ton!

(width, height) = (1300,800)
screen = pygame.display.set_mode((width, height))
playerImage = pygame.image.load('player.png')
fireballImage = pygame.image.load('fireball.png')
pygame.display.set_caption('Game')
transparent = (0, 0, 0, 0)

#Fireball stats


def player(x ,y):
 screen.blit(playerImage, (x, y))
 pygame.display.flip()

def fireball(x, y):
 screen.blit(fireballImage, (fb_x,fb_y))
 pygame.display.flip()
 fireball_state = "ready"
 print('created')
fb_x = x = width * 0.45
fb_y = y = height * 0.8
x_change = 0
y_change = 0
fb_x_change = 0
fb_y_change = 0

pressed = pygame.key.get_pressed()

#mainloop
running = True
while running:
 for event in pygame.event.get():
     if event.type == pygame.QUIT:
         running = False
     if event.type == pygame.KEYDOWN:
         if event.key == pygame.K_d:
             x_change = 1
     if event.type == pygame.KEYDOWN:
         if event.key == pygame.K_a:
             x_change = -1
     if event.type == pygame.KEYUP:
         if event.key == pygame.K_a or event.key == pygame.K_d:
             x_change = 0
     if event.type == pygame.KEYDOWN:
         if event.key == pygame.K_w:
             y_change = -2
     if event.type == pygame.KEYDOWN:
         if event.key == pygame.K_s:
             y_change = 2
     if event.type == pygame.KEYUP:
         if event.key == pygame.K_w or event.key == pygame.K_s:
             y_change = 0
     if event.type == pygame.KEYDOWN:
         if event.key == pygame.K_SPACE:
             fb_y_change = -400
     if event.type == pygame.KEYUP:
         if event.key == pygame.K_SPACE:
             fb_y_change = 0
   x += x_change
 y += y_change
 fireball(x,y)
 fb_x = x
 fb_y = y
 screen.fill((255,255,255))
 player(x,y)
 fb_x += fb_x_change
 fb_y += fb_y_change
 fireball(fb_x,fb_y)
 pygame.display.update()
  • Do you want it to move to a specific location, or move until it hits something? – afghanimah Apr 26 '20 at 09:50
  • move until it hits something is what I want to do at the end. Right now my problem is that the fireball is teleporting to the location and not actually moving slowly to it – kr4ck3d_is_s4d Apr 26 '20 at 10:04
  • You should look at @Rabbid76 answer but beyond that you should really separate you fireball's and Player (and probably later you enemies) into classes rather than having all their information (x, y, change, etc) at the top level like this. Better yet look into pygames Sprite class since it is very helpful for exactly this kind of thing. – Glenn Mackintosh Apr 26 '20 at 15:07

1 Answers1

0

First of all remove pygame.display.flip() from player and fireball. That casuse flickering. A single pygame.display.update() at the end of the application loop is sufficient.
Note, the fireballs have to be blit at (x, y) rather than (fb_x, fb_y)

def player(x ,y):
    screen.blit(playerImage, (x, y))

def fireball(x, y):
    screen.blit(fireballImage, (x, y))
    fireball_state = "ready"

Further more use pygame.time.Clock() / tick() to control the flops per second (FPS):

clock = pygame.time.Clock()

#mainloop
running = True
while running:
    clock.tick(FPS)

Add a list for the fire balls:

fireballs = []

Spawn a new fireball when space is pressed:

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_SPACE:
        fireballs.append([x, y, 0, -2])

Update and draw the fireballs in a loop:

for fb in fireballs:
    fb[0] += fb[2]
    fb[1] += fb[3]
for fb in fireballs:
    fireball(fb[0],fb[1])

See the example:

fireballs = []

FPS = 60
clock = pygame.time.Clock()
running = True
while running:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                fireballs.append([x, y, 0, -2])

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_w]:
        y -= 2
    if pressed[pygame.K_s]:
        y += 2
    if pressed[pygame.K_a]:
        x -= 2
    if pressed[pygame.K_d]:
        x += 2

    for i in range(len(fireballs)-1, -1, -1):
        fb = fireballs[i]
        fb[0] += fb[2]
        fb[1] += fb[3]
        if fb[1] < 0:
            del fireballs[i]

    screen.fill((255,255,255))
    player(x,y)
    for fb in fireballs:
        fireball(fb[0],fb[1])
    pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Hey man thanks a lot! I figured it out with the help of your code but I must admit I really wanted to resist using it at first because I was having problem understanding what the ending is exactly. Can you please explain (if you don't mind) the code from the for statement and under? Or specifically the two for statements. – kr4ck3d_is_s4d Apr 26 '20 at 16:06
  • @kr4ck3d_is_s4d `fireballs = []` is a list. `fireballs.append([x, y, 0, -2])` appends a list with 4 elements to the list. Hence you create a list and each element is a list too, thus you can shoot multiple bullets. The 4 elements of the inner list are the position and the movement vector. `for fb in fireballs:` iterates the list, `fb` is an element of the list, containing the inner list with the 4 elements. – Rabbid76 Apr 26 '20 at 16:13