How to match background color of an image with background color of Pygame? [duplicate]

2024/10/9 14:22:17

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 pygameclass DrawCharacter():bg_color = ((0, 0, 255))def __init__(self, screen):"""Initialize the superman and set starting position"""self.screen = screen# Load image and get rectself.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 screenself.rect.centerx = self.screen_rect.centerxself.rect.centery = self.screen_rect.centerydef 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 DrawCharacterdef run_game():# Initialize game and make screen objectpygame.init()screen = pygame.display.set_mode((1200, 800))pygame.display.set_caption("Blue Sky")bg_color = (0, 0, 255)# Make supermansuperman = DrawCharacter(screen)# Start the main loop for the gamewhile True:# Check for keyboard and mouse eventsfor event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()# Redraw screen during each loop passscreen.fill(bg_color)superman.blitme()# make the most recently drawn screen visiblepygame.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

Answer

if the image has a transparent background try doing the following in game_Character.py:

# Load image and get rectself.image = pygame.image.load("Images/supermen.bmp").convert_alpha()self.rect = self.image.get_rect()self.screen_rect = screen.get_rect()

If it has a white background you probably want to set the colorkey like so:

    # Load image and get rectself.image = pygame.image.load("Images/supermen.bmp").convert()self.image.set_colorkey((255, 255, 255))self.rect = self.image.get_rect()self.screen_rect = screen.get_rect()
https://en.xdnf.cn/q/118572.html

Related Q&A

Sharepoint/SOAP - GetListItems ignoring query

Trying to talk from Python to Sharepoint through SOAP.One of the lists I am trying to query contains ID as primary key field.(Field){_RowOrdinal = "0"_FromBaseType = "TRUE"_DisplayN…

python mean between file

I create a list of more than a thousand file (Basically now I have a list with the name of the file) now in order to make the man I thought to do something like this (suppose asch file have 20 lines): …

Sort a dictionary with custom sorting function

I have some JSON data I read from a file using json.load(data_file){"unused_account":{"logins": 0,"date_added": 150},"unused_account2":{"logins": 0,&qu…

Turtle make triangle different color

Hi guys Im trying to replicate this image:Its almost done I just have one issue, where the triangle is supposed to be yellow it isnt seeming to work.Mine:Code:fill(True) fillcolor(green) width(3) forwa…

How to DataBricks read Delta tables based on incremental data

we have to read the data from delta table and then we are joining the all the tables based on our requirements, then we would have to call the our internal APIS to pass the each row data. this is our g…

Converting an excel file to a specific Json in python using openpyxl library with datetime

I have the Excel data with the format shown in the image preview. How can I convert it into a JSON using Python? Expected Output: file_name = [ { A: Measurement( calculated_date=datetime(2022, 10, 1, …

How to find a word in a string in a list? (Python)

So im trying to find a way so I can read a txt file and find a specific word. I have been calling the file with myfile=open(daily.txt,r)r=myfile.readlines()that would return a list with a string for ea…

How to make a new default argument list every time [duplicate]

This question already has answers here:The Mutable Default Argument in Python(34 answers)Closed 10 years ago.I have the following setup:def returnList(arg=["abc"]):return arglist1 = returnLis…

How does one reorder information in an XML document in python 3?

Lets suppose I have the following XML structure:<?xml version="1.0" encoding="utf-8" ?> <Document><CstmrCdtTrfInitn><GrpHdr><other_tags>a</other_t…

Python - Replace only exact word in string [duplicate]

This question already has answers here:How to match a whole word with a regular expression?(4 answers)Closed 4 years ago.I want to replace only specific word in one string. However, some other words h…