Python Forum
[PyGame] Getting projectiles into pygame.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Getting projectiles into pygame.
#6
Hey boys, I've successfully created a space invaders clone with pygame thanks to you!

I've used the "list method" and if you want to use a Rect instead an Img with windows.blit, you have to write the for loop after the pygame.draw(things) like this (note I've used some variables like colors that are not declared!):

    screen.fill(bgcolor)
    pygame.draw.rect(screen, color, ship)

    for bullet in bullets:
        bullet.y -= 5
        if bullet.y < 0:
            bullet_remover.append(bullet)
        else:
            pygame.draw.rect(screen, color, bullet
    pygame.display.flip()
    clock.tick(60)
Put this after the whole thing
This is the code for the bullets, but with enemies is the same.
To CONTROL the bullet shoot, you can simply append a Rect to the list in the while loop like this:
while True:
    ticks = pygame.time.get_ticks()
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            pygame.mixer.music.stop()
            pygame.quit()
            print('Thanks for playing Space invaders! \nclosing...')
            sys.exit()

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_UP:
                if ticks > bullet_next_tick:
                    bullet_next_tick = ticks + bullet_interval
                    bullets.append(pygame.Rect(ship.x, ship.y, 5, 20))

            if event.key == pygame.K_RIGHT:
                x_spid += 5

            if event.key == pygame.K_LEFT:
                x_spid -= 5

        if event.type == pygame.KEYUP:

            if event.key == pygame.K_RIGHT:
                x_spid -= 5

            if event.key == pygame.K_LEFT:
                x_spid += 5
Obviously you still need to remove bullets
    
    for bullet in bullet_remover:
        bullets.remove(bullet)

    bullet_remover = []
I'm really happy and proud of that. Thank you. Relly, thank you.
(Tell me if you'd like to see the project ;))

(May-24-2020, 04:41 PM)lolloiltizio Wrote: Hey boys, I've successfully created a space invaders clone with pygame thanks to you!

I've used the "list method" and if you want to use a Rect instead an Img with windows.blit, you have to write the for loop after the pygame.draw(things) like this (note I've used some variables like colors that are not declared!):

    screen.fill(bgcolor)
    pygame.draw.rect(screen, color, ship)

    for bullet in bullets:
        bullet.y -= 5
        if bullet.y < 0:
            bullet_remover.append(bullet)
        else:
            pygame.draw.rect(screen, color, bullet
    pygame.display.flip()
    clock.tick(60)
Put this after the whole thing
This is the code for the bullets, but with enemies is the same.
To CONTROL the bullet shoot, you can simply append a Rect to the list in the while loop like this:
while True:
    ticks = pygame.time.get_ticks()
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            pygame.mixer.music.stop()
            pygame.quit()
            print('Thanks for playing Space invaders! \nclosing...')
            sys.exit()

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_UP:
                if ticks > bullet_next_tick:
                    bullet_next_tick = ticks + bullet_interval
                    bullets.append(pygame.Rect(ship.x, ship.y, 5, 20))

            if event.key == pygame.K_RIGHT:
                x_spid += 5

            if event.key == pygame.K_LEFT:
                x_spid -= 5

        if event.type == pygame.KEYUP:

            if event.key == pygame.K_RIGHT:
                x_spid -= 5

            if event.key == pygame.K_LEFT:
                x_spid += 5
Obviously you still need to remove bullets
    
    for bullet in bullet_remover:
        bullets.remove(bullet)

    bullet_remover = []
I'm really happy and proud of that. Thank you. Relly, thank you.
(Tell me if you'd like to see the project ;))

*really Wall
Reply


Messages In This Thread
Getting projectiles into pygame. - by Everett_ - May-19-2020, 09:07 PM
RE: Getting projectiles into pygame. - by Windspar - May-21-2020, 06:02 PM
RE: Getting projectiles into pygame. - by Everett_ - May-23-2020, 03:10 PM
RE: Getting projectiles into pygame. - by lolloiltizio - May-24-2020, 04:41 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020