1

I am a kind of big noob in coding! I actually has 3 thing in it to ask! I had watched some tutorials on making a game using python and from that i have made a game type thing! In which i have draw bullets to which i want to change to image and it fires all at a click i also wnat to fire 1 bullet a time abd also i want my character to do a double jump.Thanks for visit!Please help me out!

import pygame
pygame.init()

class projectile(object):
    def __init__(self,x,y,radius,color,facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.facing = facing
        self.vel = 8 * facing

    def draw(self,win):
        pygame.draw.circle(win, self.color, (self.x,self.y), self.radius)



def redrawGameWindow():
    win.blit(bg, (0,0))
    man.draw(win)
    for bullet in bullets:
        bullet.draw(win)
    
    pygame.display.update()


#mainloop
man = player(200, 410, 64,64)
bullets = []
run = True
while run:
    clock.tick(27)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        
    for bullet in bullets:
        if bullet.x < 500 and bullet.x > 0:
            bullet.x += bullet.vel
        else:
            bullets.pop(bullets.index(bullet))

    keys = pygame.key.get_pressed()

    if keys[pygame.K_c]:
        if man.left:
            facing = -1
        else:
            facing = 1
            
        if len(bullets) < 3:
            bullets.append(projectile(round(man.x + man.width //2), round(man.y + man.height//2), 6, (0,0,0), facing))

    if keys[pygame.K_a] and man.x > man.vel:
        man.x -= man.vel
        man.left = True
        man.right = False
        man.standing = False
    elif keys[pygame.K_d] and man.x < 500 - man.width - man.vel:
        man.x += man.vel
        man.right = True
        man.left = False
        man.standing = False
    else:
        man.standing = True
        man.walkCount = 0
        
    if not(man.isJump):
        if keys[pygame.K_SPACE]:
            man.isJump = True
        if man.left:
            facing = -1
        else:
            facing = 1
            man.walkCount = 0
    else:
        if man.jumpCount >= -8:
            neg = 1
            if man.jumpCount < 0:
                neg = -1
            man.y -= (man.jumpCount ** 2) * 0.5 * neg
            man.jumpCount -= 1
        else:
            man.isJump = False
            man.jumpCount = 8
            
    redrawGameWindow()

pygame.quit()

1 Answers1

2

If you want to fire just 1 bullet when c is pressed, then implement a KEYDOWN event (see pygame.event):

while run:
    # [...]

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_c:
                facing = -1 if man.left else 1
                if len(bullets) < 3:
                    startPos = round(man.x + man.width //2), round(man.y + man.height//2)
                    bullets.append(projectile(startPos, 6, (0,0,0), facing))

    # [...]

    # DELETE
    #if keys[pygame.K_c]:
    #    if man.left:
    #        facing = -1
    #    else:
    #        facing = 1
    #        
    #    if len(bullets) < 3:
    #        bullets.append(...)

The state keys[pygame.K_c] is True as long the key is hold down. This causes that 1 bullet is fired in every frame as long the key is pressed. The KEYDOWN event is executed only once when a key is pressed.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Sorry but i got an error! ``` facing = -1 if man.left else 1 ^ IndentationError: expected an indented block ``` –  Sep 01 '20 at 14:25
  • @Pranish There was an indentation error in my answer. I've fixed it right now. The indentation of the 4 lines after `if event.key == pygame.K_c:` was not correct. – Rabbid76 Sep 01 '20 at 14:27