Pygame get_rect() is not running

Question:

get_rect() is not running

I was trying to make a simple game for educational purposes using the pygame module. I encountered this error. I would be glad if you can help

import pygame 
import random
import sys 
import os 

pygame.init()
balikci_konum = "E:/E/Python/Python-eski/Oyuncalismalari/balik_avlama_oyunu/textures/balikci.png"
genislik = 1000
yukseklik = 600 

ekran = pygame.display.set_mode((genislik,yukseklik))

# ...

class Balik(pygame.sprite.Sprite):
    def __init__(self,x,y,resim,tip):
        super().__init__()
        self.image = resim 
        self.rect = self.image.get_rect()
        self.rect.topleft = (x,y)
        self.tip = tip 
        self.hiz = random.randint(0,13)
        self.yonx= random.choice([1,-1])
        self.yony= random.choice([1,-1])
        
    def update(self):
        self.rect.x = self.hiz*self.yonx
        self.rect.y = self.hiz*self.yony
        
        if self.rect.left <=0 or self.rect.right >= genislik : 
            self.yonx *=-1
            
        if self.rect.top <=0 or self.rect.bottom >= yukseklik : 
            self.yony *=-1
     
#Balık Grupları 
balik1 = pygame.image.load("E:/E/Python/Python-eski/Oyuncalismalari/balik_avlama_oyunu/textures/balik1.png")
balik2 = pygame.image.load("E:/E/Python/Python-eski/Oyuncalismalari/balik_avlama_oyunu/textures/balik2.png")

balik_grup = pygame.sprite.Group()
balik = Balik(random.randint(0,genislik-32),random.randint(0,yukseklik-32),balik1,0)
balik_grup.add(balik)

balik = Balik(random.randint(0,genislik-32),random.randint(0,yukseklik-32),balik2,0)
balik_grup.add(balik)

# ...

that is all of my code
i tried relocating objects but still get_rect() function doesn’t work

Asked By: xYusufX

||

Answers:

To move the objects you have to change the position with += instead of setting the position with an assignment (=):

self.rect.x = self.hiz*self.yonx
self.rect.y = self.hiz*self.yony

self.rect.x += self.hiz*self.yonx
self.rect.y += self.hiz*self.yony
Answered By: Rabbid76
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.