Krishnakant Sati
Krishnakant Sati

Reputation: 1

Pygame rectangle/bullet not being appended to a list or even spawned

I created a list to see whether any rectangles(bullets) are being spawned when I press Left CTRL(for the player or plyr), or Right CTRL (for the other player, referred to as enmy). But nothing is being appended to the list

import pygame as pg
import os

WIDTH,HEIGHT=900,500
FPS = 60
WIN = pg.display.set_mode((WIDTH,HEIGHT))
pg.display.set_caption("Death")
ship_size = (55,40)

yellship = pg.transform.rotate(pg.transform.scale(pg.image.load(
    os.path.join('Assets','spaceship_yellow.png')),ship_size),90)
plyr_strt = (200,200)
plyr_hp = 1
plyr_vel = 3
plyr_bullet_cap = 10
plyr_bullet_vel = 7

redship = pg.transform.rotate(pg.transform.scale(pg.image.load(
    os.path.join('Assets','spaceship_red.png')),ship_size),270)
enmy_strt = (600,200)
enmy_hp = 1
enmy_vel = 3
enmy_bullet_cap = 10
enmy_bullet_vel = 7

#IMPORTANT A LITTLE FURTHER DOWN*

def Player_Controls(keys_pressed,player):
    if keys_pressed[pg.K_a]:
        player.x -= plyr_vel
    if keys_pressed[pg.K_d]:
        player.x += plyr_vel
    if keys_pressed[pg.K_w]:
        player.y -= plyr_vel
    if keys_pressed[pg.K_s]:
        player.y += plyr_vel
        
def Enemy_Controls(keys_pressed,enemy):
    if keys_pressed[pg.K_LEFT]:
        enemy.x -= enmy_vel
    if keys_pressed[pg.K_RIGHT]:
        enemy.x += enmy_vel
    if keys_pressed[pg.K_UP]:
        enemy.y -= enmy_vel
    if keys_pressed[pg.K_DOWN]:
        enemy.y += enmy_vel

#Main game loop
def main():
    plyr = pg.Rect(plyr_strt,ship_size)
    enmy = pg.Rect(enmy_strt,ship_size)
    plyr_bullets = []
    enmy_bullets = []
    run = True
    clock = pg.time.Clock()
    while run:
        clock.tick(FPS)
        for event in pg.event.get():
            if event.type == pg.QUIT:
                run = False

            # RIGHT HERE IS THE PROBLEM
            if event.type == pg.KEYDOWN:
                if event.type == pg.K_LCTRL:
                    plyr_bullet = pg.Rect(plyr.x + ship_size[1],plyr.y + ship_size[0]/2,10,5)
                    plyr_bullets.append(plyr_bullet)

                if event.type == pg.K_RCTRL:
                    enmy_bullet = pg.Rect(enmy.x,enmy.y + ship_size[0]/2,10,5)
                    enmy_bullets.append(enmy_bullet)
        
        print(enmy_bullets,plyr_bullets)
        keys_pressed = pg.key.get_pressed()
        Player_Controls(keys_pressed,plyr)
        Enemy_Controls(keys_pressed,enmy)
        draw_win(plyr,enmy)

    pg.quit()

if __name__ == "__main__":
    main()

There are no problems with the images I'm using.

I'm all ears to any suggestions.

Please don't be too harsh on me, still just a newbie when it comes to python and pygame.

Upvotes: 0

Views: 27

Answers (1)

Cristian Ramon-Cortes
Cristian Ramon-Cortes

Reputation: 1888

The issue looks a small "typo" when checking the pressed key.

On the one hand, event.type returns the type of the event. In your example, the only event type you are interested in is KEYDOWN. On the other hand, event.key returns the key that triggered the event. In your example, the only keys you are interested in are K_LCTRL and K_RCTRL.

Thus, your code should look like:

# RIGHT HERE IS THE PROBLEM
if event.type == pg.KEYDOWN:
    if event.key == pg.K_LCTRL:
        # Actions when left control is pressed
    if event.key == pg.K_RCTRL:
        # Actions when right control is pressed

Upvotes: 1

Related Questions