I need to Make a class that draws the character at the
center of the screen and match the background color of the image to the background color of the screen, or vice versa. I have already set a value for the background color of Pygame screen to blue, but when I do the same in my DrawCharacter
class, it just opens the screen with the background color of the image (white). I might just be placing the bg_color
attribute at the wrong place in my class.
game_character.py
import sys
import pygame
class DrawCharacter():
bg_color = ((0, 0, 255))
def __init__(self, screen):
"""Initialize the superman and set starting position"""
self.screen = screen
# Load image and get rect
self.image = pygame.image.load("Images/supermen.bmp")
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# Start each new supermen at the center of the screen
self.rect.centerx = self.screen_rect.centerx
self.rect.centery = self.screen_rect.centery
def blitme(self):
"""Draw the superman at its current location"""
self.screen.blit(self.image, self.rect)
blue_sky.py
import sys
import pygame
from game_character import DrawCharacter
def run_game():
# Initialize game and make screen object
pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Blue Sky")
bg_color = (0, 0, 255)
# Make superman
superman = DrawCharacter(screen)
# Start the main loop for the game
while True:
# Check for keyboard and mouse events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Redraw screen during each loop pass
screen.fill(bg_color)
superman.blitme()
# make the most recently drawn screen visible
pygame.display.flip()
run_game()
I expected the background color of the image to be the same as the background color of the Pygame screen, but it is not. The first block of code is for my class file, while the second is for the pygame file