Python Forum
[PyGame] Getting projectiles into pygame.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Getting projectiles into pygame.
#1
Hey all,

I'm really new to Python, this is only really my 5th or 6th day using the language (or really any major language). When I was younger I used Scratch a lot so if I'm using the wrong terminology or I am being unclear I'm sorry I can clear things up if you would like me to:).

Right now this I'm trying to create a space-invaders-type clone, this is my first project without the direct help of a guide. So I want to have bullets come out of my plane sprite. I'm trying to figure out how to do two things really, one is to get a 'clone' of the bullet sprite to spawn at an x and y relative to the plane. I know how to use .blit((x, y)) but I can't figure out how to get a clone of the sprite to spawn in so I can have multiple bullets coming out of the plane. The next thing I want to figure out is how to get that new spawned clone of the sprite to keep going towards y = 0 and stopping either when it gets there or collides with the enemy(ies)

I've googled some guides and posts and I either can't figure out how it could apply to my project or I can't understand what is happening. I saw a lot of ideas including classes and lists and while I understand what lists are I don't see/understand how they apply here, I am also starting to understand the usefulness of classes but they still are a bit confusing to me.
Thanks for any help:)

# imports and initializations
import pygame
import random
pygame.init()



# game start

running = True


gametick = 100
px = 250 # player x and y
py = 275
pvel = 25 # player velocity and bullet vel
bvel = 40
mod = 1
clock = pygame.time.Clock()

# window
window_height = 600
window_width = 800
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("Game")


# sprite imports.
playerImg = pygame.image.load("images\player.png")
backgroundImg = pygame.image.load("images\Background.png")
bulletImg = pygame.image.load("images\Bullet.png")




# Definitions

def player(px, py):
    window.blit(playerImg, (px, py))
    pygame.transform.scale(playerImg, (10, 10))


def background():
    window.blit(backgroundImg, (0, 0))


def borders():
    global px
    if px <= 0:
        px = 0
    elif px >= 704:
        px = 704



# Gameloop
while running:

    clock.tick(30)
    # Stop command
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    # Showing sprites and background
    background()
    player(px, py)
    # bullets
    # nothing here yet

    # Controls
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        borders()
        px -= pvel * mod
    if keys[pygame.K_RIGHT]:
        borders()
        px += pvel * mod
    if keys[pygame.K_UP]:
        pass # this will be for the bullets


    pygame.display.update()
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

Forum Jump:

User Panel Messages

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