Error in Pygame code: 'int' object has no attribute 'rect'

Asked 2 years ago, Updated 2 years ago, 82 views

I'd like to write a winning code.

This is a program in which the player returns to its initial position when it hits en1 moving by operating the player with the cross key.

"I would like to say ""If player and en1 get hit,"" but I get an error in the following areas."
What should I do?

Where applicable:

 if self.rect.colliderect(self.en1.rect):

error messages:

 'int' object has no attribute 'rect'

I'm a beginner, and I'm doing it by imitation, so I think there are a lot of useless things, but I appreciate your cooperation.

Reference Page:
https://algorithm.joho.info/programming/python/pygame-blockout/

Current State Code:

import pygame
from pygame.locals import*
import sys
pygame.mixer
import random

SCREEN= Rect (1500, 800, 0, 32)

# cloud
classClSprite(pygame.sprite.sprite):
def__init__(self, filename, x, y, vx, vy):
    pygame.sprite.sprite.__init__(self,self.containers)
    self.image=pygame.image.load(filename).convert_alpha()
    self.rect=self.image.get_rect()
    self.rect.center= [x,y]
    self.vx = vx
    self.vy =vy
    w=self.image.get_width()
default (self):
    self.rect.move_ip(self.vx,self.vy)
    if self.rect.right<0:
        self.rect.left=1500
   
# Player
class PlayerSprite(pygame.sprite.sprite):
def__init__(self, filename, en1, x, y, vx, via, angle=0):
    pygame.sprite.sprite.__init__(self,self.containers)
    self.image=pygame.image.load(filename).convert_alpha()
    self.rect=self.image.get_rect()
    self.rect.center= [x,y]
    self.vx = vx
    self.vy =vy
    self.angle=angle
    ifangle!=0:self.image=pygame.transform.rotate(self.image,angle)
    self.jump=pygame.mixer.Sound("j2.mp3")
    self.deth=pygame.mixer.Sound("aa.wav")
    w=self.image.get_width()
    h=self.image.get_height()
    self.rect=Rect(x,y,w,h)   
    self.en1 = en1

def Rmove(self):
    self.rect.move_ip(30,0)
def Lmove(self):
    self.rect.move_ip (-30,0)
def Umove(self):
    self.rect.move_ip(0,-70)
default (self):
    self.rect.move_ip(self.vx,self.vy)
    if self.rect.left<0:
       self.rect.left = 0
    if self.rect.top<0:
       self.rect.top = 0
    if self.rect.top>810:
       self.de()
    if self.rect.top>930:
       pygame.quit()
       sys.exit()
       
    if self.rect.colliderect(self.en1.rect):
     self.rect.left = 0 
def de(self):
    self.deth.play(1)
def bjump(self):
    self.jump.play(1)    

# En1
class En1Sprite(pygame.sprite.Sprite):
def__init__(self, filename, xy, vxy, angle=0):
    x,y = xy
    vx,vy = vxy
    pygame.sprite.sprite.__init__(self,self.containers)
    self.image=pygame.image.load(filename).convert_alpha()
    self.rect=self.image.get_rect()
    w=self.image.get_width()
    h=self.image.get_height()
    self.rect=Rect(x,y,w,h)
    self.vx = vx
    self.vy =vy
    self.angle=angle
    ifangle!=0:self.image=pygame.transform.rotate(self.image,angle)

default (self):
    self.rect.move_ip(self.vx,self.vy)
    if self.rect.top<0 or self.rect.bottom>850:
       self.vy = -self.vy

defmain():
    pygame.init()
screen=pygame.display.set_mode(1500,801),0,32)
screen=pygame.display.get_surface()
pygame.display.set_caption("Test") 

bg = pygame.image.load("img.jpg").convert_alpha()
rect_bg = bg.get_rect()

   # SUPERITE GROUP
    group=pygame.sprite.RenderUpdates()
en1 = pygame.sprite.Group() 

En1Sprite.containers=group,en1
PlayerSprite.containers=group
ClSprite.containers=group

    #sprite
    s=-10
for min range (7):
    m = random.randint (0,800)
    for n in range (300):
     n = random.randint (200,1000000)
     en1 = En1 Sprite ("to.PNG", (n, m), (s, 3), 0)
en1 = En1 Sprite ("to.PNG", (4400, 0), (-4, 3), 0)

player=PlayerSprite ("m.png", 0,600, 0,5,0)

cloud=ClSprite ("clo.png", 700, 600, s, 0)
cloud=ClSprite ("clo.png", 900, 100, s, 0)
cloud=ClSprite ("clo.png", 300, 500, s, 0)
cloud=ClSprite ("clo.png", 100, 300, s, 0)

clock = pygame.time.clock()

while(1):
    clock.tick(30)
    screen.fill(0,100,0,0)) 
    screen.blit(bg, rect_bg) 
    group.update()
    group.draw(screen)
    pygame.display.update()  

    for event in pygame.event.get():
        if event.type==pygame.KEYDOWN: 
           if event.key==pygame.K_LEFT:
             player.Lmove()
           if event.key==pygame.K_RIGHT:
             player.Rmove()
           if event.key==pygame.K_UP:
             player.Umove()
             player.bjump()
          
        if event.type==QUIT:
            pygame.quit()
            sys.exit()          
        
if__name__=="__main__":
        main() 

python pygame

2022-09-30 11:43

1 Answers

Assuming that the source code of the question has been corrected correctly and that the problem can now be reproduced, the reasons may be as follows:
(All the source codes quoted are sources that do not seem to have the correct indentation at this time.)

The second initialization parameter for PlayerSprite in the line below appears to be a simple integer 0 rather than an object containing pygame.Rect (probably an instance of class En1Sprite).

player=PlayerSprite("m.png", 0,600, 0,5,0)

The relevant source for the PlayerSprite class is as follows, assuming that en1 has a variable rect in en1 in update() where the integer 0 was specified during initialization:

#Player
class PlayerSprite(pygame.sprite.sprite):
def__init__(self, filename, en1, x, y, vx, via, angle=0):
                  ...omitted in the middle...
    self.en1 = en1
                  ...omitted in the middle...
default (self):
                  ...omitted in the middle...
    if self.rect.colliderect(self.en1.rect):
                  ...abbreviated from now on...

That's probably why self.en1 with integers substituted does not have an attribute called rect.

I'm not sure if it's correct, but you might want to try using the en1 created in the line above it as a parameter.

 en1 = En1 Sprite ("to.PNG", (4400, 0), (-4, 3), 0)

#### player=PlayerSprite("m.png", 0,600, 0,5,0)### Original line
player=PlayerSprite("m.png", en1,600,0,5,0)####Change new line 0 to en1

However, what happens because the process of substituting a number of En1Sprite objects in the previous double for loop into the variable en1 with the same name is unknown? Alternatively, it is possible that objects will be substituted again for en1 outside the loop, which will cause other symptoms, such as not happening what you intended.

#sprite
    s=-10
for min range (7):
    m = random.randint (0,800)
    for n in range (300):
     n = random.randint (200,1000000)
     en1 = En1 Sprite ("to.PNG", (n, m), (s, 3), 0)


2022-09-30 11:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.