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

查看:67
本文介绍了Pygame (Python) - TypeError: Argument must be rect style object的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很令人沮丧,我正在尝试在 Pygame (Python) 中制作一个破砖游戏,但我遇到了一个错误.过去几个小时我一直在努力解决这个问题,但我仍然没有.

This is quite frustrating, I am trying to make a brick breaker game in Pygame (Python) But I am stuck at an error. I have been trying for the past several hours to solve this, but I still have not.

所以请尝试帮助我,我真的很想继续编程,而不是被这个错误卡住......

So please try and help me, I would really like to continue programming this, instead of being stuck at this error...

代码:

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 = WHITE

font = 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()
                return

rb = pygame.image.load('redblock.png')

rbrect = rb.get_rect().topleft = (0, 0)
rb1rect = rb.get_rect().topleft = (40, 0)
rb2rect = rb.get_rect().topleft = (80, 0)
level1rects = [rbrect, rb1rect, rb2rect]

moveleft = False
moveright = False
SPEED = 7

bmoveup = bmovedown = bmoveleft = bmoveright = False
BALLSPEED = 8

mainwindow.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 = 1
    for 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 = True
                moveright = False
            if event.key == ord('d') or event.key == K_RIGHT:
                moveleft = False
                moveright = True
            if event.key == ord('g'):
                bmoveup = True
                if number == 1:
                    bmoveleft = True
                else:
                    bmoveright = True
        if event.type == KEYUP:
            if event.key == ord('a') or event.key == K_LEFT:
                moveleft = False
            if event.key == ord('d') or event.key == K_RIGHT:
                moveright = False

    if moveleft and paddlerect.left > 0:
        paddlerect.left -= SPEED
    if moveright and paddlerect.right < WINDOWWIDTH:
        paddlerect.right += SPEED

    if bmovedown and ballrect.bottom < WINDOWHEIGHT:
        ballrect.top += BALLSPEED
    if bmoveup and ballrect.top > 0:
        ballrect.top -= BALLSPEED
    if bmoveleft and ballrect.left > 0:
        ballrect.left -= BALLSPEED
    if bmoveright and ballrect.right < WINDOWWIDTH:
        ballrect.right += BALLSPEED

    if ballrect.top <= 0:
        bmovedown = not bmovedown
        bmoveup = not bmoveup
    if ballrect.left <= 0:
        bmoveleft = not bmoveleft
        bmoveright = not bmoveright
    if ballrect.right >= WINDOWWIDTH:
        bmoveleft = not bmoveleft
        bmoveright = not bmoveright
    if ballrect.bottom >= WINDOWHEIGHT:
        bmovedown = not bmovedown
        bmoveup = not bmoveup

    mainwindow.fill(WHITE)
    mainwindow.blit(paddle, paddlerect)
    mainwindow.blit(ball, ballrect)


    for b in range(len(level1rects)):
        mainwindow.blit(rb, level1rects[b])


    for x in level1rects:
        if ballrect.colliderect(x):
            level1rects.remove(x)
            bmovedown = not bmovedown
            bmoveup = not bmoveup
            bmoveleft = not bmoveleft
            bmoveright = not bmoveright


    if ballrect.colliderect(paddlerect):
        bmovedown = not bmovedown
        bmoveup = not bmoveup
        bmoveleft = not bmoveleft
        bmoveright = not bmoveright




    pygame.display.update()
    fpsclock.tick(35)

错误:

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

推荐答案

试试这个代码:

rb = pygame.image.load('redblock.png')

rbrect = rb.get_rect()
rbrect.topleft = (0, 0)
rb1rect = rb.get_rect()
rb1rect.topleft = (40, 0)
rb2rect = rb.get_rect()
rb2rect.topleft = (80, 0)

这篇关于Pygame (Python) - TypeError: Argument must be rect style object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆