TypeError: Argument must be rect style object - Pygame (Python [duplicate]

2024/11/8 11:15:50

Possible Duplicate:
Pygame (Python) - TypeError: Argument must be rect style object

I am trying to make a brick breaker game in Pygame (with Python) but I am stuck at an error.

(this is random text that is necessary so I can post all this code)

CODE:

import pygame, sys, time, random
from pygame.locals import *
pygame.init()
fpsclock = pygame.time.Clock()WINDOWWIDTH = 450
WINDOWHEIGHT = 650
mainwindow = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Luzion - Brick Breaker')paddle = pygame.image.load('Brick Breaker - Paddle.png')
paddlerect = paddle.get_rect()
paddlerect.topleft = (190, 575)ball = pygame.image.load ('ball.png')
ballrect = ball.get_rect()
ballrect.topleft = (195, 565)cooltext = pygame.image.load('cooltext1.png')
cooltextrect = cooltext.get_rect()
cooltextrect.topleft = (0, 0)BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 128, 0)
BLUE = (0, 0, 255)
LIME = (0, 255, 0)
TEXTCOLOR = WHITEfont = pygame.font.SysFont(None, 48)def displaytext(text, font, surface, x, y):text = font.render(text, 1, TEXTCOLOR)textrect = text.get_rect()textrect.topleft = (x, y)surface.blit(text, textrect)def waitforplayer():while True:for event in pygame.event.get():if event.type == QUIT:pygame.quit()sys.exit()if event.type == KEYDOWN:if event.key == K_ESCAPE:pygame.quit()sys.exit()returnrb = pygame.image.load('redblock.png')
rb2 = rb1 = rb
level1blocks = [rb, rb1, rb2]
rbrect = rb.get_rect().topleft = (0, 0)
rb1rect = rb1.get_rect().topleft = (40, 0)
rb2rect = rb2.get_rect().topleft = (80, 0)
level1rects = [rbrect, rb1rect, rb2rect]moveleft = False
moveright = False
SPEED = 7bmoveup = bmovedown = bmoveleft = bmoveright = False
BALLSPEED = 8mainwindow.blit(cooltext, cooltextrect)
pygame.display.update()
time.sleep(1)displaytext('Level 1', font, mainwindow, 150, 100)
pygame.display.update()
time.sleep(1)displaytext('Press any key to begin...', font, mainwindow, 22, 200)
pygame.display.update()
waitforplayer()while True:number = 1for event in pygame.event.get():if event.type == QUIT:pygame.quit()sys.exit()if event.type == KEYDOWN:if event.key == ord('a') or event.key == K_LEFT:moveleft = Truemoveright = Falseif event.key == ord('d') or event.key == K_RIGHT:moveleft = Falsemoveright = Trueif event.key == ord('g'):bmoveup = Trueif number == 1:bmoveleft = Trueelse:bmoveright = Trueif event.type == KEYUP:if event.key == ord('a') or event.key == K_LEFT:moveleft = Falseif event.key == ord('d') or event.key == K_RIGHT:moveright = Falseif moveleft and paddlerect.left > 0:paddlerect.left -= SPEEDif moveright and paddlerect.right < WINDOWWIDTH:paddlerect.right += SPEEDif bmovedown and ballrect.bottom < WINDOWHEIGHT:ballrect.top += BALLSPEEDif bmoveup and ballrect.top > 0:ballrect.top -= BALLSPEEDif bmoveleft and ballrect.left > 0:ballrect.left -= BALLSPEEDif bmoveright and ballrect.right < WINDOWWIDTH:ballrect.right += BALLSPEEDif ballrect.top <= 0:bmovedown = not bmovedownbmoveup = not bmoveupif ballrect.left <= 0:bmoveleft = not bmoveleftbmoveright = not bmoverightif ballrect.right >= WINDOWWIDTH:bmoveleft = not bmoveleftbmoveright = not bmoverightif ballrect.bottom >= WINDOWHEIGHT:bmovedown = not bmovedownbmoveup = not bmoveupmainwindow.fill(WHITE)mainwindow.blit(paddle, paddlerect)mainwindow.blit(ball, ballrect)for b in range(len(level1blocks)):mainwindow.blit(level1blocks[b], level1rects[b])for i in level1rects[:]:if ballrect.colliderect(i):level1blocks.remove(i)level1rects.remove(i)bmovedown = not bmovedownbmoveup = not bmoveupbmoveleft = not bmoveleftbmoveright = not bmoverightif ballrect.colliderect(paddlerect):bmovedown = not bmovedownbmoveup = not bmoveupbmoveleft = not bmoveleftbmoveright = not bmoverightpygame.display.update()fpsclock.tick(35)

ERROR:

Traceback (most recent call last):File "C:/Python32/brick breaker", line 140, in <module>if ballrect.colliderect(i):
TypeError: Argument must be rect style object

I have no clue how to solve this error, please help.

Answer

The problem is that pygame expects everything in the array level1rects to be a rectange, and in this case, you must have something in that array that isn't.

https://en.xdnf.cn/q/120626.html

Related Q&A

How to compare dates in python and find the greater one

I want to compare 2 date and predict a label true if date 1 greater than date 2 and predict false date 1 less than date 2. I have trained the model but model is predicting wrong for near by dates that …

Python: is isalnum() the same as isalpha with isdigit?

Is there a way to concretely verify this? I tried to solve a coding question but it seems one of the test cases (not revealed to me) takes this as wrong. In what kinds of cases does this fail to be tr…

Python code works fine first time, but fails second time

The first time I run this block of code from Notebook it works fine:#Which letters and how many letters = ["a","b","c"] noOfLetters = len(letters)#Looking for all permutat…

How do I subtract a value in the dictionary? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

CSV IO python: converting a csv file into a list of lists

How to convert a csv file into a list of lists where each line is a list of entries with in a bigger list?Im having trouble with this because some of my entries have a comma in thema file like: 1,2,3…

Sheet of paper in millimeters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

Kivy App build with Buildozer. APK crash

I am using Oracle VirtualBox running Ubuntu 16. I have been able to build apk files for a while until my latest build. My program will run and keep its functionality when run with python 2.7 on the sam…

Python3 - convert csv to json using pandas

Ive got a .csv files with 5 columns but I only need the json file to contain 3 of these how would i go about doing it?csv file:Ncode Ocode name a b c 1 1.1 1x 1a 1b 1…

Python List of Dictionaries by Loops

I have 2 python list of dictionaries:[{index:1,color:red},{index:2,color:blue},{index:3,color:green} ]and[{device:1,name:x},{device:2,name:y},{device:3,name:z} ]How can I append each dictionary from th…

Removing parentheses and comma

Im importing Data from a database into python data frame. Now, I wish to use the data for further analysis, however, I need to do a little cleaning of the data before using. Currently, the required col…