python - argument must be rect style object - Pygame -
disclaimer: know other people on site have had error it's been in different contexts , fixes don't seem work here.
hey stackoverflow, i've been making sideways space invaders game, ship follows mouse's y position stays on left, , clicking shoots bullet. enemies spawn randomly right , fly left, , objects removed once each side of screen. works fine, when tried implement collision detection see when shot hits enemy, error:
typeerror: argument must rect style object.
it points line "return hitbox.colliderect(target.rect)". try , work out causing made 'print' lines below see 'target.rect' (and whether proper rect object), , seemed same style shot's own rect, i'm lost why colliderect doesn't accept it.
i've annotated pretty everything, sorry if it's insult intelligence should make things easier.
class causes problems:
class gameobject: def __init__(self, image, height, speed): self.speed = speed self.image, self.rect = load_image("shot.dsf", -1) #loads image , creates rectange self.pos = image.get_rect().move(0, height) def move(self): self.pos = self.pos.move(self.speed, 0) #moves along self.rect = [self.pos, 32, 32] #updates position of rectange. without rectangle stays in top left corner image moves if self. pos.right > 1400: #checks if shot goes offscreen objects.remove(self) #removes def hitreg(self,target): hitbox = self.rect #creates hitbox of it's own rectangle return hitbox.colliderect(target.rect) #should return true if hitbox collides rect of target, returns error print("own rect: ",self.rect) #debug purposes, returns ('own rect: [<rect(1350, 245, 32, 32)>, 32, 32]) print("target rect: ",target.rect) #debug purposes, returns ('target rect: [<rect(360, 479, 128, 64)>, 128, 64])
main loop
r.drawframe() #renders frame while running: sleep(0.0166) #make sure renders @ 60fps rand = rnd(10,20) #generates random number 10 20 pos = pygame.mouse.get_pos() #gets mouse position event in pygame.event.get(): if event.type == mousebuttonup: #prevents hang none if event.type == mousebuttondown: #creates shot if mousebuttondown, @ mouse height random speed o = gameobject(shot, pos[1], rand) objects.append(o) if rnd(1,120) == 60: #randomely spawns enemy ships s = enemyship(enemya, rnd(10,890), rand) #enemya filename ships.append(s) o in objects: #for each shot s in ships: #for each enemy ship if o.hitreg(s): #calls hitreg function, s being target s.takedamage() score = score + 10 #increases score r.drawframe() #renders frame
if output shown in source code can trusted, debug statements show neither self.rect
nor target.rect
"rect style" objects. appear lists consisting of rect followed 2 integers such this:
own rect: [<rect(1350, 245, 32, 32)>, 32, 32]) target rect: [<rect(360, 479, 128, 64)>, 128, 64]
the move()
method responsible changing self.rect
"normal" rect list above, modified self.rect
not behave rect. perhaps in hitreg()
should calling hitbox = self.rect[0]
.
anyway, in case, suspect output of debug statements shown incorrect. value self.rect
rect
(because lists not have method colliderect()
, colliderect()
seem called), , target.rect
list. target.rect
of type enemyship
, don't show code that, not possible tell going on there (although 1 guess move()
has been called on , it's self.rect
corrupted).
i suggest capture real debug output , add question. helpful if post accurate code, because there @ least 1 syntax error, e.g. in line:
if self. pos.right > 1400:
and code enemyship
like?
Comments
Post a Comment