Search code examples
pygamepygame-surface

I'm trying to draw a rocket on the screen


Screen just appears black.

I've checked if the self.screen is working, and it does. Can't find a reason for why the ship is not showing up.

import sys

import pygame

from settings import Settings
from rocket import Rocket


class RocketMan:
    """Overall class to manage game assets and behavior"""

    def __init__(self):
        """Initialize the game and create game resources"""
        pygame.init()

        self.clock = pygame.time.Clock()
        self.settings = Settings()

        self.screen = pygame.display.set_mode(
            (self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Rocket man")

        self.rocket = Rocket(self)

    def run_game(self):
        """Start the game"""
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

            # Redraw the screen each pass through the loop
            self.screen.fill(self.settings.bg_color)

            # Draw rocket
            self.rocket.blitme()

            # Make the most recent screen visible
            pygame.display.flip()
            self.clock.tick(60)


if __name__ == "__main__":
    """Make a game instance and run the game"""
    ai = RocketMan()
    ai.run_game()

Rocket file: I've checked the .bmp file for the rocket, and it's working fine.

Also checked that background is appearing before the ship and after the screen flip.

import pygame

class Rocket:
    """A class to manage the rocket"""

    def __init__(self, ai_game):
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()

        # Load the image to the screen
        self.image = pygame.image.load("spiked_blue_ship.bmp")
        self.rect = self.image.get_rect()

        # Start the rocket from the bottom of the screen
        self.rect.midtop = self.screen_rect.midbottom

    def blitme(self):
        """Draw the rocket at its current location"""
        self.screen.blit(self.image, self.rect)

Solution

  • Your Rocket object is not drawn above the bottom of the screen but just below it. The rocket is not visible because it is outside the screen. Change

    self.rect.midtop = self.screen_rect.midbottom

    self.rect.midbottom = self.screen_rect.midbottom