Softwareprogrammer94
Softwareprogrammer94

Reputation: 53

I can't see bullet image

I want to create my own game for using reinforcement learning and I created this code. But when I press space bar I can't see the bullet.

Can anyone help? Thanks for interest My codes is this:

import pygame
import random

WIDTH = 800
HEIGHT = 600

First I want to create my player, but I want to create my bullet image on my player class too, that's why I do this:

class Player(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('warship.png').convert()
        self.image2 = pygame.image.load('bullet.png').convert()
        self.rect = self.image.get_rect()
        self.bullet_rect = self.image2.get_rect()
        self.bullet_rect.centerx = self.rect.centerx
        self.bullet_rect.y = self.rect.y
        self.bullet_rect_speedy = 0
        self.rect.centerx = WIDTH/2
        self.rect.bottom = HEIGHT - 50
        self.speedx = 0

Then I want to move my player and bullet together:

    def update(self):
        self.speedx = 0
        self.bullet_rect_speedy = 0
        keystate = pygame.key.get_pressed()

        if keystate[pygame.K_LEFT]:
            self.speedx = -4
        elif keystate[pygame.K_RIGHT]:
            self.speedx = 4
        elif keystate[pygame.K_SPACE]:
            self.bullet_rect_speedy = -10
        else:
            self.speedx = 0

        self.rect.x += self.speedx
        self.bullet_rect.y += self.bullet_rect_speedy


        if self.rect.right > WIDTH:
            self.rect.right = WIDTH
        if self.rect.left < 0:
            self.rect.left = 0

        if self.bullet_rect.y <= 0:
            self.bullet_rect.y = self.rect.y

    def getCoordinates(self):
        return (self.rect.x,self.rect.y)


pygame.init()

screen = pygame.display.set_mode((WIDTH,HEIGHT))

background = pygame.image.load('yeni_resim.jpg')

pygame.display.set_caption('Space Invaders')

icon = pygame.image.load('spaceship.png')

pygame.display.set_icon(icon)

all_sprite = pygame.sprite.Group()

player = Player()

all_sprite.add(player)

running = True

while running:  

    screen.blit(background,(0,0))

    all_sprite.update()
    all_sprite.draw(screen)

    pygame.display.flip()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        pygame.quit()
        break

Then I start to code it's working, but I can't see bullet image. Interestingly no error is produced. Can someone please help me. İ am so new to this. Thanks for your interest.

Upvotes: 1

Views: 58

Answers (1)

Kingsley
Kingsley

Reputation: 14916

The bullet is not being drawn to the screen because Sprite objects only draw their internal sprite.image at the location in sprite.rect. The player image is in Player.image, the bullet is in Player.image2. Pygame.Sprite doesn't know anything about image2, and ignores it.

You could implement your own Player.draw() that blits both the images. But the best solution is to separate the bullet out into it's own class:

class Bullet(pygame.sprite.Sprite): 
    def __init__(self):
        pygame.sprite.Sprite.__init__( self )
        self.image = pygame.image.load('bullet.png').convert()
        self.rect  = self.image.get_rect()
        self.rect.center = ( x, y )
        self.velocity    = 1

    def update( self ):
        x, y = self.rect.center    # get current position
        y -= self.velocity         # move up (negative-Y is up)
        if ( y < 0 ):
            self.kill()            # went off top of screen
        else:
            self.rect.center = ( x, y )


class Player(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('warship.png').convert()
        self.rect  = self.image.get_rect()
        self.rect.centerx = WIDTH/2
        self.rect.bottom  = HEIGHT - 50
        self.speedx       = 0

    def update(self):
        self.speedx = 0
        keystate = pygame.key.get_pressed()

        if keystate[pygame.K_LEFT]:
            self.speedx = -4
        elif keystate[pygame.K_RIGHT]:
            self.speedx = 4
        elif keystate[pygame.K_SPACE]:
            # Create a new bullet just above the player
            global all_bullets_group
            x, y = self.rect.center
            new_bullet = Bullet( x, y-5 )
            all_bullets_group.add( new_bullet )  

        [...]

Upvotes: 2

Related Questions