E
E
Egor Bochkarev2019-08-21 09:11:58
Game development
Egor Bochkarev, 2019-08-21 09:11:58

How to make bullets in pygame platform fire vertically?

I just started learning how to program, and the bullets shoot normally left and right, but up and down refuse completely.

Here is the whole code:
import pygame

pygame.init()

screen = pygame.display.set_mode((1920, 1080))

pygame.display.set_caption('My game')

left = False
right = False
up = False
down = False

x = 5
y = 540

wide = 300
tall = 300
speed = 5

person1right = pygame.image.load('1.png')
person1up = pygame.image.load('1.1.png')
person1left = pygame.image.load('1.2.png')
person1down = pygame.image.load('1.3.png')
back = pygame.image.load('back.png')

class Bullet():

    def __init__(self, x, y, radius, color, facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.facing = facing
        self.speed = facing * 15

    def draw(self, screen):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius)
lastmove = 1
run = True
clock = pygame.time.Clock()

def draw():
    screen.blit(back, (0, 0))

    if right:
        screen.blit(person1right, (x, y))
    if left:
        screen.blit(person1left, (x, y))
    if up:
        screen.blit(person1up, (x, y))
    if down:
        screen.blit(person1down, (x, y))
    for bullet in bullets:
        bullet.draw(screen)

    pygame.display.update()
bullets = []
run = True
while run == True:
    clock.tick(60)
    pygame.time.delay(5)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_s] and y<1920 - tall -5:
        y += speed
        left = False
        right = False
        up = False
        down = True
    if keys[pygame.K_w] and y>5:
        y -= speed
        left = False
        right = False
        up = True
        down = False
    if keys[pygame.K_a] and x>5:
        x -= speed
        left = True
        right = False
        up = False
        down = False
    if keys[pygame.K_d] and x<1920 - wide -5:
        x += speed
        left = False
        right = True
        up = False
        down = False

    if right == True:
        lastmove == 1
    elif left== True:
        lastmove == 2
    elif up == True:
        lastmove == 3
    elif down == True:
        lastmove == 4
    if keys[pygame.K_f]:
        if len(bullets) <= 100:
            bullets.append(Bullet(round(x + wide // 2), round(y + tall // 2 ), 5, (255, 0, 0), facing ))
    if right:
        facing = 1
    if down:
        facing = 1
    if left:
        facing = -1
    if up:
        facing = -1

    for bullet in bullets:
        if bullet.x > 0 and bullet.x < 1920:
            if lastmove == 1 or lastmove == 2:
                bullet.x += bullet.speed
        if lastmove == 3 or lastmove == 4:
                bullet.y += bullet.speed
        if bullet.x < 0 or bullet.x >1920:
            bullets.pop(bullets.index(bullet))
    keys = pygame.key.get_pressed()
    draw()
pygame.quit()
Bullet part:
class Bullet():

    def __init__(self, x, y, radius, color, facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.facing = facing
        self.speed = facing * 15

if keys[pygame.K_d] and x<1920 - wide -5:
        x += speed
        left = False
        right = True
        up = False
        down = False


    if right == True:
        lastmove == 1
    elif left== True:
        lastmove == 2
    elif up == True:
        lastmove == 3
    elif down == True:
        lastmove == 4
    if keys[pygame.K_f]:
        if len(bullets) <= 100:
            bullets.append(Bullet(round(x + wide // 2), round(y + tall // 2 ), 5, (255, 0, 0), facing ))
    if right:
        facing = 1
    if down:
        facing = 1
    if left:
        facing = -1
    if up:
        facing = -1

    for bullet in bullets:
        if bullet.x > 0 and bullet.x < 1920:
            if lastmove == 1 or lastmove == 2:
                bullet.x += bullet.speed
        if lastmove == 3 or lastmove == 4:
                bullet.y += bullet.speed
        if bullet.x < 0 or bullet.x >1920:
            bullets.pop(bullets.index(bullet))

I can't figure it out for a long time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dollar, 2019-08-21
@Joynear

The process of finding bugs is called " debugging ". As a programmer, you must first master this skill. It doesn't matter if you are a beginner or not.
In programming in general, in particular in the Python language, and even more so in the Pygame game engine, it is convenient to use the console to display debug information, namely the print() function .
I will not, and will not be able to debug through the answers on this resource. This is your task. Here you can sometimes meet telepaths and clairvoyants, but only in simple obvious questions, where there are three lines of code. But only you can handle the debugging. Because it's basically trial and error. You need to check, check, check, experiment until the error is localized.
An example of how debugging could be done via the console in your case:

for bullet in bullets:
        print("lastmove:", lastmove)
        if bullet.x > 0 and bullet.x < 1920:
            if lastmove == 1 or lastmove == 2:
                bullet.x += bullet.speed
        if lastmove == 3 or lastmove == 4:
                bullet.y += bullet.speed
        if bullet.x < 0 or bullet.x >1920:
            print("bullets.pop!!!")
            bullets.pop(bullets.index(bullet))

But where, what and how you will check - you decide.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question