Error: descriptor blit requires a pygame.Surface object but received a NoneType [duplicate]

2024/9/22 10:33:50

I am creating a game. I wrote this code to create a sprite and its hitbox:

hg = pygame.image.load('hgrd1copy.jpg').set_colorkey(red)
hgbox = pygame.Rect(0 ,13 ,36 ,72)
pygame.Surface.blit(hg, hgbox)

I had originally put

windowSurface.blit(hg, hgbox)

but then I got an error telling me that my argument needed to be pygame.Surface not none.

However, when I changed the code to pygame.Surface.blit, it gives me this error code: TypeError: descriptor 'blit' requires a 'pygame.Surface' object but received a 'NoneType' What do I do?

Answer

As explained in the commemts - y9u have to load your image in one line of code, and in another line call the set_colorkey method on the image read. And them, blit should be called as a method from an existing surface - not from the Surface class. (With the code as it is now in the question, how would the program "know" where to blit the image to?

So, assuming your screen is on the windowSurface variable you describe, this should work:

hg = pygame.image.load('hgrd1copy.jpg')
hg.set_colorkey(red)
hgbox = pygame.Rect(0, 13, 36, 72)
windowSurface.blit(hg, hgbox)
https://en.xdnf.cn/q/119146.html

Related Q&A

How can I deal with overlapping rectangles? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

panda df not showing all rows after loading from MS SQL

Im using Pandas with latest sqlalchemy (1.4.36) to query a MS SQL DB, using the following Python 3.10.3 [Win] snippet: import pandas as pd # from sqlalchemy…

How to extract multiple grandchildren/children from XML where one child is a specific value?

Im working with an XML file that stores all "versions" of the chatbot we create. Currently we have 18 versions and I only care about the most recent one. Im trying to find a way to extract a…

Is there are a way to replace python import with actual sources?

I am having python files with the import statements which I would like to replace into the actual code placed in the foo.py.For instance, with the in file:from foo import Barbar = Bar() print barI woul…

How to use supported numpy and math functions with CUDA in Python?

According to numba 0.51.2 documentation, CUDA Python supports several math functions. However, it doesnt work in the following kernel function: @cuda.jit def find_angle(angles):i, j = cuda.grid(2)if i …

PYTHON - Remove tuple from list if contained in another list

I have a list of tuples:list_of_tuples = [(4, 35.26), (1, 48.19), (5, 90.0), (3, 90.0)]tuple[0] is an item_IDtuple[1] is an angleI have a list of item_IDs I want to remove/ignore from the list:ignore_I…

How to run a ij loop in Python, and not repeat (j,i) if (i,j) has already been done?

I am trying to implement an "i not equal to j" (i<j) loop, which skips cases where i = j, but I would further like to make the additional requirement that the loop does not repeat the perm…

Split a string with multiple delimiters

I have the string "open this and close that" and I want to obtain "open this and" and "close that". This is my best attempt:>>>print( re.split(r[ ](?=(open|clos…

Extracting a string between 2 chracters using python [duplicate]

This question already has answers here:Python-get string between to characters(4 answers)Closed 7 years ago.I need a Python regex to give me all the strings between ~ and ^ from a string like this:~~~~…

remove empty line printed from hive query output using python

i am performing a hive query and storing the output in a tsv file in the local FS. I am running a for loop for the hive query and passing different parameters. If the hive query returns no output once …