【问题标题】:type object Player has no attribute recttype object Player 没有属性 rect
【发布时间】:2015-02-28 19:58:59
【问题描述】:

我目前正在编写一个游戏,其中您的播放器是 png 文件,并且您必须避免使用石头。我不断收到错误“类型对象播放器没有属性 rect”。我是 OOP 的新手,所以任何帮助都将不胜感激!代码如下:

import pygame
import os
import random
import sys

random.seed()

_image_library = {}
def get_image(path):
    global _image_library
    image = _image_library.get(path)
    if image == None:
        canonicalized_path = path.replace('/', os.sep).replace('\\', os.sep)
        image = pygame.image.load(canonicalized_path)
        _image_library[path] = image
    return image

y = 250

class Player(object):

    def __init__(self):
        self.rect = pygame.Rect(0, y, 50, 50)

    def move(self, dx, dy):

    # Move each axis separately. Note that this checks for collisions both times.
        if dx != 0:
            self.move_single_axis(dx, 0)
        if dy != 0:
            self.move_single_axis(0, dy)

    def move_single_axis(self, dx, dy):

    # Move the rect
        self.rect.x += dx
        self.rect.y += dy

pygame.init()
screen = pygame.display.set_mode((500, 500))
done = False
clock = pygame.time.Clock()

rockXList = []
rockYList = []

y = 250
xrand = 750
speed = 1
loop = 0

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP]:
        if(y > 3):
            y -= 3
    if pressed[pygame.K_DOWN]:
        if(y < 497):
            y += 3

    a = 0
    while(a < len(rockXList)):
        rx = rockXList[a]
        rx = rx - speed
        rockXList[a] = rx
        a = a + 1

    screen.fill((153, 76, 0))
    screen.blit(get_image("race-toad.png"), (0, y))
    i = 0
    while(i < len(rockXList)):
        rx = rockXList[i]
        ry = rockYList[i]
        pygame.draw.rect(screen, (160, 160, 160), pygame.Rect(rx, ry, 32, 32))
        execstr = "end_rect"+str(i)+" = pygame.Rect(rx, ry, 32, 32)"
        exec execstr
        i = i + 1

    b = 0
    while(b < len(rockXList)):
        rx = rockXList[b]
        ry = rockYList[b]
        name = "if Player.rect.colliderect(end_rect" + str(b) + "):\n   speed = 0\n print 'YOU LOSE!'"
        exec name
        b = b + 1

    xadd = random.randint(10, 125)
    rockX = xrand + xadd
    xrand = rockX
    rockY = random.randint(0, 450)

    rockXList.append(rockX)
    rockYList.append(rockY)

    if(loop > 600):
        speed = speed + 1
        loop = 0
    else:
        loop = loop + 1

    pygame.display.flip()
    clock.tick(60)

【问题讨论】:

    标签: python oop python-2.7 pygame


    【解决方案1】:

    我不是 OOP 方面的专家,但据我所知,问题是您没有创建 Player() 类的 INSTANCE,相反,您只创建了该类本身.

    在 OOP 中,(自定义)类是一种结构,可让您创建自定义对象,但在创建该类的 INSTANCE 之前,您并没有真正创建任何内容。

    我认为解决方案是在初始化 pygame 后添加类似的内容:

      player = Player()
    

    那么你每次引用“Player”时都应该用“player”替换,因为“Player()”是CLASS,而“player”是实际的INSTANCE > 您将在游戏中使用。

    希望我说的是正确和有道理的。

    另外,我认为您应该了解 pygame.sprite 模块,因为它似乎适合您想要实现的目标。

    【讨论】:

    • 欢迎您,记住您可以通过投票或/和标记为“正确答案”或类似的方式来帮助我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-21
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 2022-11-27
    • 2021-03-09
    相关资源
    最近更新 更多