Pygame - TypeError: invalid rect assigment - python

So I'm trying to make a platformer game. Since I'm new to pygame library, I'm following a video tutorial(https://www.youtube.com/playlist?list=PL8ui5HK3oSiGXM2Pc2DahNu1xXBf7WQh-). And I ran to a problem. I checked documentation, tutorials, I even found somebody here that asked the same question. But nothing worked. So what did I do wrong and how can I repair it?
Player class:
import pygame
class Player(pygame.sprite.Sprite):
def __init__(self,pos):
super().__init__()
self.image = pygame.Surface((32,32))
self.image.fill((0,0,255))
self.rect = self.image.get_rect(topleft = pos)
This is level code in which I'm drawing the player now. I will change it.
import pygame
from tiles import Tile
from settings import tileSize
from player import Player
class Level:
#setup
def __init__(self, levelData, surface):
self.displaySurface = surface
self.setupLevel(levelData)
self.worldShift = 0
def setupLevel(self,layout):
self.tiles = pygame.sprite.Group()
self.player = pygame.sprite.GroupSingle()
for rowIndex, row in enumerate(layout):
for colIndex, col in enumerate(row):
x = colIndex * tileSize
y = row * tileSize
if col == 'X':
tile = Tile((x,y),tileSize)
self.tiles.add(tile)
if col == 'P':
playerSprite = Player((x,y))
self.player.add(playerSprite)
def run(self):
#level tiles
self.tiles.update(self.worldShift)
self.tiles.draw(self.displaySurface)
#player
self.player.draw(self.displaySurface)
This is error message:
Hello from the pygame community. https://www.pygame.org/contribute.htmlTraceback (most recent call last):
File "d:\AdkoaMisko-game\main\window.py", line 10, in <module>
level = Level(levelMap,screen)
File "d:\AdkoaMisko-game\main\level.py", line 10, in __init__
self.setupLevel(levelData)
File "d:\AdkoaMisko-game\main\level.py", line 25, in setupLevel
playerSprite = Player((x,y))
File "d:\AdkoaMisko-game\main\player.py", line 8, in __init__
self.rect = self.image.get_rect(topleft = pos)
TypeError: invalid rect assignment

row is a list of columns. The index of the row is rowIndex:
y = row * tileSize
y = rowIndex * tileSize

Related

Pong Game From Python for absolute beginners

i am running into two error going two routes the ball is find works great but the score text wont work, every time i implement it it throws the two errors.
from superwires import games, color
from random import *
"""Main application file"""
#the screen width and height
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
points = 0
score = 0
"""Paddle object that is controlled by the player. Moves up and down. Collides with the ball. Points are added when ball is collided with"""
class Paddle(games.Sprite):
HEIGHT = 480/20
paddle_image = games.load_image('paddle.png', transparent=True)
def __init__(self):
super(Paddle, self).__init__(image = Paddle.paddle_image,
x = 40,
y = 50,
is_collideable=True)
self.score = games.Text(value = 0,
size = 30,
color = color.green,
x = SCREEN_WIDTH - 25,
y = SCREEN_HEIGHT - 431)
games.screen.add(self.score)
games.screen.mainloop()
""" updates Position based on mouse Position"""
def update(self):
self.y = games.mouse.y
self.Check_Collision()
def Check_Collision(self):
for PongBall in self.overlapping_sprites:
PongBall.handle_collide()
"""actual ball in play """
class PongBall(games.Sprite):
#velocity variables
vel_x = 5
vel_y = 0
#pos of boundry to ball pos
lower_bounds_check = 0
upper_bounds_check = 0
#checks if paddle gets collided with
def handle_collide(self):
if((SCREEN_WIDTH/2) < self.x):
PongBall.vel_x = randint(-10,-5)
PongBall.vel_y = randint(-10,-5)
elif((SCREEN_WIDTH/2) > self.x):
PongBall.vel_x = randint(5,10)
PongBall.vel_y = randint(5,10)
#checks if it hit the bottom; bool function
def check_boundry_lower(self):
PongBall.lower_bounds_check = (SCREEN_HEIGHT/2)
return ((self.y - PongBall.upper_bounds_check) > PongBall.lower_bounds_check)
#checks if it hits the top; bool function
def check_boundry_upper(self):
PongBall.upper_bounds_check = (SCREEN_HEIGHT/2)
return ((self.y + PongBall.upper_bounds_check) < PongBall.upper_bounds_check)
#Resets the velocity based on boundry collision
def boundry_update(self):
if(self.check_boundry_upper()):
PongBall.vel_y = randint(5,10)
return
elif(self.check_boundry_lower()):
PongBall.vel_y = randint(-10,-5)
return
def update(self):
self.boundry_update()
self.x += PongBall.vel_x
self.y += PongBall.vel_y
"""
Entry point Function
"""
# Create background, paddle, and ball. Then call the game mainloop.
# crate the window
games.init(SCREEN_WIDTH, SCREEN_HEIGHT, 50, virtual = False)
#sets/loads background
background = games.load_image(f"background.png", transparent=True)
games.screen.background = background
# paddle image
paddle_image = games.load_image(f"paddle.png", transparent = True)
#PingPong ball
PingPongBall = games.load_image(f"Ball.png", transparent = True)
#paddle Right and left
paddle_1 = Paddle()
paddle_2 = Paddle()
#pingball ball instance
ball = PongBall(image = PingPongBall, x = 150, y = 250, is_collideable=True)
#adding all three sprites to screen
games.screen.add(ball)
games.screen.add(paddle_1)
games.screen.add(paddle_2)
#each instance update function
ball.update()
paddle_1.update()
paddle_2.update()
#adding text
#main loop
games.screen.mainloop()
i get this error;
Traceback (most recent call last):
File "C:\Users\gamer\Desktop\PingPong\main.py", line 15, in <module>
class Paddle(games.Sprite):
File "C:\Users\gamer\Desktop\PingPong\main.py", line 17, in Paddle
paddle_image = games.load_image('paddle.png', transparent=True)
File "C:\Users\gamer\AppData\Local\Programs\Python\Python310\lib\site-packages\superwires\games.py", line 836, in load_image
if not screen.virtual:
AttributeError: 'NoneType' object has no attribute 'virtual'
Please help i cant seem to escape the error even if i dont pass through the constructor i get text object doesnt not have handle_collide() attribute
-Thanks guys
new error:
Traceback (most recent call last):
File "C:\Users\gamer\Desktop\PingPong\main.py", line 153, in <module>
games.screen.mainloop()
File "C:\Users\gamer\AppData\Local\Programs\Python\Python310\lib\site-packages\superwires\games.py", line 783, in mainloop
sprite._process_sprite()
File "C:\Users\gamer\AppData\Local\Programs\Python\Python310\lib\site-packages\superwires\games.py", line 440, in _process_sprite
self.update()
File "C:\Users\gamer\Desktop\PingPong\main.py", line 40, in update
self.Check_Collision()
File "C:\Users\gamer\Desktop\PingPong\main.py", line 36, in Check_Collision
PongBall.handle_collide()
AttributeError: 'Text' object has no attribute 'handle_collide'
You need to have the games.load in the __init__ function so as not to call it before the environment is set up.
Replace this:
class Paddle(games.Sprite):
HEIGHT = 480/20
paddle_image = games.load_image('paddle.png', transparent=True)
def __init__(self):
super(Paddle, self).__init__(image = Paddle.paddle_image,
x = 40,
y = 50,
is_collideable=True)
with this:
class Paddle(games.Sprite):
HEIGHT = 480/20
def __init__(self):
self.paddle_image = games.load_image('paddle.png', transparent=True)
super(Paddle, self).__init__(image = self.paddle_image,
x = 40,
y = 50,
is_collideable=True)

Black BG during render under tiles, looks fine in Tiled map editor

I started learning Pygame and Tiled map editor. I have the following test:
Example
It looks fine in the editor, the objects that have the black BG are currently on layer_4 (but they do it regardless of layer count).
I already tried using convert_alpha on it, ticking and unticking the transparency color in Tiled when loading in the tile map.
This is the original picture:
Full picture
This is the part where I load in the picture:
def import_cut_graphics(path):
surface = pygame.image.load(path).convert_alpha()
tile_num_x = int(surface.get_size()[0] / TILESIZE)
tile_num_y = int(surface.get_size()[1] / TILESIZE)
cut_tiles = []
for row in range(tile_num_y):
for col in range(tile_num_x):
x = col * TILESIZE
y = row * TILESIZE
new_surface = pygame.Surface((TILESIZE, TILESIZE))
new_surface.blit(surface, (0, 0), pygame.Rect(x, y, TILESIZE, TILESIZE))
cut_tiles.append(new_surface)
return cut_tiles
And this is where I work with it:
class Level:
def __init__(self, level_data, surface):
# general setup
self.all_layers = import_cut_graphics("img\ProjectUtumno_full.png")
self.display_surface = surface
self.world_shift = 0
# terrain setup
layer1_layout = import_csv_layout(level_data["layer_1"])
self.layer1_sprites = self.create_tile_group(layer1_layout, 'layer_1')
# grass setup
layer2_layout = import_csv_layout(level_data["layer_2"])
self.layer2_sprites = self.create_tile_group(layer2_layout, "layer_2")
# crates
layer3_layout = import_csv_layout(level_data["layer_3"])
self.layer3_sprites = self.create_tile_group(layer3_layout, "layer_3")
# layer 4
layer4_layout = import_csv_layout(level_data["layer_4"])
self.layer4_sprites = self.create_tile_group(layer4_layout, "layer_4")
def create_tile_group(self, layout, type):
sprite_group = pygame.sprite.Group()
for row_index, row in enumerate(layout):
for col_index, val in enumerate(row):
if val != '-1':
x = col_index * TILESIZE
y = row_index * TILESIZE
if type == 'layer_1':
tile_surface = self.all_layers[int(val)]
sprite = StaticTile(TILESIZE, x, y, tile_surface)
sprite_group.add(sprite)
if type == "layer_2" :
tile_surface = self.all_layers[int(val)]
sprite = StaticTile(TILESIZE, x, y, tile_surface)
sprite_group.add(sprite)
if type == "layer_3" :
tile_surface = self.all_layers[int(val)]
sprite = StaticTile(TILESIZE, x, y, tile_surface)
sprite_group.add(sprite)
if type == "layer_4" :
tile_surface = self.all_layers[int(val)]
sprite = StaticTile(TILESIZE, x, y, tile_surface)
sprite_group.add(sprite)
return sprite_group
def run(self):
# run the entire game / level
self.camera()
# layer1
self.layer1_sprites.update(self.world_shift)
self.layer1_sprites.draw(self.display_surface)
# layer2
self.layer2_sprites.update(self.world_shift)
self.layer2_sprites.draw(self.display_surface)
# layer3
self.layer3_sprites.update(self.world_shift)
self.layer3_sprites.draw(self.display_surface)
# layer4
self.layer4_sprites.update(self.world_shift)
self.layer4_sprites.draw(self.display_surface)
This is my main game loop:
class Game:
def __init__(self):
pygame.init()
pygame.font.init()
self.screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
self.clock = pygame.time.Clock()
self.screen_name = pygame.display.set_caption("New Game")
self.running = True
self.playing = False
self.level = Level(level_0, self.screen)
self.character_spritesheet = Spritesheet('img\MainCharacter\B_witch_idle.png')
def main(self):
# game loop
while self.running:
self.events()
self.update()
self.draw()
self.running = False
def draw(self):
self.screen.fill(BLACK)
self.level.run()
self.all_sprites.draw(self.screen)
self.clock.tick(FPS)
pygame.display.update()
I did try commenting the screen fill black and got the same result. I tried out set_colorkey with white/black colors but didn't work out.
I add my tile create class too:
class Tile(pygame.sprite.Sprite):
def __init__(self, size, x, y):
super().__init__()
self.image = pygame.Surface((size, size))
self.rect = self.image.get_rect(topleft=(x, y))
def update(self, shift):
self.rect.x += shift
class StaticTile(Tile):
def __init__(self, size, x, y, surface):
super().__init__(size, x, y)
self.image = surface
Thanks in advance if you can figure out why i have this problem.
So for anyone who's having this problem, I managed to solve it by this:
Inside my import_cut_graphics methode I added a set_colorkey(BLACK), turns out the problem occured when I was originally cutting the picture, therefore it didnt matter how much I change it at the latter phases since the problem was in an earlier stage.
def import_cut_graphics(path):
surface = pygame.image.load(path).convert_alpha()
tile_num_x = int(surface.get_size()[0] / TILESIZE)
tile_num_y = int(surface.get_size()[1] / TILESIZE)
cut_tiles = []
for row in range(tile_num_y):
for col in range(tile_num_x):
x = col * TILESIZE
y = row * TILESIZE
new_surface = pygame.Surface((TILESIZE, TILESIZE))
new_surface.blit(surface, (0, 0), pygame.Rect(x, y, TILESIZE, TILESIZE))
new_surface.set_colorkey(BLACK)
cut_tiles.append(new_surface)
return cut_tiles

A variable which suppose to be pygame.Surface appears like a string

I just added a function to my code which should display an image from a directory. It requires an argument which specifies which window it displays it in. When I try to pass it in I receive the following error:
Traceback (most recent call last):
File "Pygame.py", line 122, in <module>
Player.load()
File "Pygame.py", line 74, in load
screen.blit(self.path, (self.x, self.y))
TypeError: argument 1 must be pygame.Surface, not str
My code:
import pygame
#init the pygame and create a screen
pygame.init()
screen = pygame.display.set_mode((1080,720))
done = False
#colours
blue = (0,0,255)
red = (255,0,0)
green = (0,255,0)
black = (0,0,0)
white = (255,255,255)
yellow = (255,255,0)
#path to the background
bg_path = "Racing.png"
#path to the car image
car_path = "car.png"
#starts the game clock
clock = pygame.time.Clock()
#opening bg image
background_image = pygame.image.load(bg_path).convert()
#class for all of the objects on the screen
class shape():
def __init__(self, place, x, y):
self.place = place
self.x = x
self.y = y
class rectangle(shape):
def __init__(self, place, colour, x, y, length, width):
super().__init__(place,x, y)
self.colour = colour
self.length = length
self.width = width
def draw(self):
pygame.draw.rect(screen, self.colour, pygame.Rect(self.x, self.y,
self.length, self.width))
def move_up(self):
self.y = self.y - 10
def move_down(self):
self.y = self.y + 10
def move_right(self):
self.x = self.x + 10
def move_left(self):
self.x = self.x - 10
class player(shape):
def __init__(self, place, x, y, length, width, path):
super().__init__(place,x, y)
self.length = length
self.width = width
self.path = path
def load(self):
screen.blit(self.path, (self.x, self.y))
Rectangle = rectangle(screen, yellow, 540, 660, 60, 60)
Player = player(screen, 540, 600, 60, 60, car_path)
Player.load()
This isn't all of the code but the rest isn't related to the problem (I think). Please tell me if more code is needed.
car_path is set as a string here
car_path = "car.png"
But blit() requires a pygame.Surface object for the first argument which pygame.image.load would give you, i.e.
car_path = pygame.image.load("car.png")
instead.

Pygame throws "TypeError" when creating a custom sprite class

I am trying to create a scenery with images moving in different speeds from one side of the screen to the other, creating a parallax effect. I use a sprite class I called BackgroundObject, but when I try to create a Sprite using BackgroundObject, I am getting a TypeError.
The code I'm using is as follows:
import pygame
from pygame.locals import *
import sys, random, os
import vector
# Background objects (for parallax)
class BackgroundObject(pygame.sprite.Sprite):
def _init_(self, x, y, image, speed):
pygame.sprite.Sprite._init_(self)
self.image = image
self.rect = self.image.get_rect()
self.rect.topleft = (x,y)
self.speed = speed
def update(self, time_passed):
moved_distance = time_passed * self.speed
self.rect.left += moved_distance
if self.rect.right < 0:
self.rect.left= self.rect.width
pygame.init()
screen = pygame.display.set_mode((768, 1024), 0, 32)
obstacleImage = "obstacle.png"
fenceImage = "fences.png"
cloudImage = "clouds.png"
cityImage = "cityscape.png"
groundImage = "bottom.png"
backgroundImage = "background.png"
pathName = "Assets"
# Sprites in the game
obstacles = pygame.sprite.Group()
backgrounds = pygame.sprite.OrderedUpdates()
backgroundOrder = []
backdrop = pygame.image.load(os.path.join(pathName + os.sep + backgroundImage))
fences = pygame.image.load(os.path.join(pathName + os.sep + fenceImage))
clouds = pygame.image.load(os.path.join(pathName + os.sep + fenceImage))
cityscape = pygame.image.load(os.path.join(pathName + os.sep + cityImage))
ground = pygame.image.load(os.path.join(pathName + os.sep + cityImage))
sprite = BackgroundObject(1280, 192, clouds, 192)
backgrounds.add(sprite)
backgrounds.add(BackgroundObject(0, 192, clouds, 192))
backgrounds.add(BackgroundObject(832, 768, cityscape, 154))
backgrounds.add(BackgroundObject(0, 768, cityscape, 154))
backgrounds.add(BackgroundObject(1728, 768, fences, 384))
backgrounds.add(BackgroundObject(0, 768, fences, 384))
backgrounds.add(BackgroundObject(992, 896, ground, 512))
backgrounds.add(BackgroundObject(0, 896, ground, 512))
titleScreen = pygame.image.load(os.path.join(pathName + backgroundImage))
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
screen.blit(titleScreen, titleScreen.rect)
backgrounds.update(time_passed)
backgrounds.draw()
for item in background:
if item.rect.right < 0:
item.rect.right += (item.rect.width * 2)
The error message I am recieving is as follows, and I can't make sense out of it:
Traceback (most recent call last):
File "test.py", line 45, in <module>
sprite = BackgroundObject(1280, 192, clouds, 192)
File "/usr/lib/python2.7/dist-packages/pygame/sprite.py", line 114, in __init__
if groups: self.add(groups)
File "/usr/lib/python2.7/dist-packages/pygame/sprite.py", line 129, in add
else: self.add(*group)
File "/usr/lib/python2.7/dist-packages/pygame/sprite.py", line 129, in add
else: self.add(*group)
TypeError: add() argument after * must be a sequence, not int
You misspelled __init__; use two underscores before and after:
class BackgroundObject(pygame.sprite.Sprite):
def __init__(self, x, y, image, speed):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = self.image.get_rect()
self.rect.topleft = (x,y)
self.speed = speed
Your traceback shows BackgroundObject() goes straight to Sprite.__init__ without first passing through your method.

AttributeError: object has no attribute rect

I get an error from my code and I don't know why, but syntactically, it's correct, I guess
import pygame
class BaseClass(pygame.sprite.Sprite):
allsprites = pygame.sprite.Group()
def __init__(self, x, y, width, height, image_string):
pygame.sprite.Sprite.__init__(self)
BaseClass.allsprites.add(self)
self.image = pygame.image.load("Images\lebron.png")
self.rectangle = self.image.get_rect()
self.rectangle.x = x
self.rectangle.y = y
self.width = width
self.height = height
class Lebron(BaseClass):
List = pygame.sprite.Group()
def __init__(self, x, y, width, height, image_string):
BaseClass.__init__(self, x, y, width, height, image_string)
Lebron.List.add(self)
self.velx = 0
def movement(self):
self.rectangle.x += self.velx
so if i compile this to my main file, code below
import pygame, sys
from classes import *
from process import process
pygame.init()
WIDTH, HEIGHT = 640, 360
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
FPS = 24
lebron = Lebron(0, 100, 104, 120, "Images\lebron.png")
while True:
process()
#LOGIC
lebron.movement()
#LOGIC
#DRAW
BaseClass.allsprites.draw(screen)
screen.fill((0,0,0))
pygame.display.flip()
#DRAW
clock.tick(FPS)
I get an error which is attribute error: Lebron object has no attribute rect. what's wrong? The traceback:
Traceback (most recent call last):
File "MAIN.py", line 24, in <module>
BaseClass.allsprites.draw(screen)
File "C:\Python27\lib\site-packages\pygame\sprite.py", line 409, in draw
self.spritedict[spr] = surface_blit(spr.image, spr.rect)
AttributeError: 'Lebron' object has no attribute 'rect'
It appears from the posted traceback (they look much better when you indent them by 4 spaces, and even better when you edit the question to include them) that the PyGame draw() method expects your attribute to provide a rect attribute. At a guess, if you replace every occurrence of rectangle with rect you will make at least some progress.
Basically, PyGame wants to draw your object but it can't find out how.

Categories