0

when i run this i get the error "TypeError: argument 1 must be pygame.Surface, not pygame.Rect" which many people seem to get from using circles and stuff but I'm not using any rects or circles in this code at all. so I'm not sure where the rect is coming from. not seen a duplicate of this where they have only used surfaces either.

note that this code requires a png called "missing.png" in the directory its in - can be anything doesn't matter what it actually is.

import pygame
from sys import exit

pygame.init()

screenwidth = 1000
screenheight = 500
fps = 32
screen = pygame.display.set_mode((screenwidth,screenheight))
clock = pygame.time.Clock()

class sprite():
    def __init__(self,
                 position = (0,0),#top left
                 frames = 1,
                 finalsize = (100,100),
                 pngname="missing.png",
                 startingframe = 1):
        self.position = position
        self.frames = frames
        self.finalsize = finalsize
        self.pngname = pngname
        self.currentframe = startingframe
        
        self.immage = pygame.image.load(self.pngname)
        #dimentions defined, self.framesurface
        #transparency

        totalwidth = self.immage.get_width()
        self.frameheight = self.immage.get_height()
        self.framewidth = totalwidth/self.frames 
        self.framesurface = pygame.Surface((self.framewidth,self.frameheight))#makes surface with frame dimentions
        self.framesurface.convert_alpha()
        self.framesurface.fill((0,0,0,0))
        
        clear_surface = pygame.Surface((totalwidth,self.frameheight))
        clear_surface = clear_surface.convert_alpha()
        clear_surface.fill((0,0,0,0))#transparent surface made
        self.immage = clear_surface.blit(self.immage,(0,0),(0,0,finalsize[0],finalsize[1]))

        #self.immage
        #self.framesurface 
        
        
                                        
        
    def frame_render(self):
        self.immage = self.framesurface.blit(self.immage,#error happening on this line
                                        (0,0),
                                        (0, self.framewidth*self.currentframe, self.framewidth, self.frameheight)) #blits one frame
        self.immage = pygame.transform.scale(self.immage,self.finalsize)#scales immage to finalsize
        self.immage = screen.blit(self.immage,self.position,(0,0,self.framewidth,self.frameheight))
        
            
   
test = sprite()

while True:
    clock.tick(fps)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()



    screen.fill((255,255,255))

    test.frame_render()
    pygame.display.update()
Doggoluvr
  • 65
  • 5
  • Usually, error messages come with a line number where the error occurred, so maybe you should check that? – kenntnisse Jan 22 '23 at 23:12
  • i did it came on line 48, the first line of the frame render function. – Doggoluvr Jan 22 '23 at 23:19
  • 3
    `self.immage = self.framesurface.blit(...` is assigning a Rect to `self.immage` (blit returns a Rect) but you are treating `self.immage` like a Surface in following lines – Iain Shelvington Jan 22 '23 at 23:25
  • why does it do that??? the blit function is meant to combine surfaces so why does it turn it into a rect. and how do i avoid that – Doggoluvr Jan 22 '23 at 23:53
  • 1
    Look at the documentation for [`Surface.blit()`](https://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit), the returned `Rect` is the affected pixels. Remove the assignment, i.e., just call `self.framesurface.blit(…)`. You'll also need to remove the subsequent assignment on `screen.blit(…)`. – import random Jan 23 '23 at 00:34
  • The `blit` returns the rectangular area effect by the operation, it does not create a new `pygame.Surface` object. – Rabbid76 Jan 23 '23 at 05:44

0 Answers0