微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AttributeError:'pygame.Rect'对象没有属性'up'

如何解决AttributeError:'pygame.Rect'对象没有属性'up'

我遇到一个问题,我似乎无法在屏幕上上下移动字符。

运行代码并按向下键时,出现此错误

AttributeError: 'pygame.Rect' object has no attribute 'down'

运行代码并按向上键时,出现此错误

AttributeError: 'pygame.Rect' object has no attribute 'up'

我的代码

#! python3

import pygame

class Ship():
    """A class intended for the management of a spacecraft"""

    def __init__(self,ai_game):
        """Spaceship initialization and its initial position."""

        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.screen_rect = ai_game.screen.get_rect()

        #Load a spaceship image and retrieve its rectangle.
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()

        #Each new ship appears in the bottom center of the screen.
        self.rect.midbottom = self.screen_rect.midbottom

        #The ship's horizontal position is stored as a floating point number.
        self.x = float(self.rect.x)
        self.y = float(self.rect.y)

        #Options that indicate the movement of the ship
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False

    def update(self):
        """Update the ship's position based on the option that indicates its movement."""

        #Updating the X coordinate of the ship,not its rectangle
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.x += self.settings.ship_speed
        if self.moving_left and self.rect.left > 0:
            self.x -= self.settings.ship_speed

       
        if self.moving_up and self.rect.up > 0:
            self.y += self.settings.ship_speed
        if self.moving_down and self.rect.down > self.screen_rect.down:
            self.y -= self.settings.ship_speed

        
        #Updating rect based on self.x
        self.rect.x = self.x
        self.rect.y = self.y

    def blitme(self):
        """displaying a spaceship in its current position."""
        self.screen.blit(self.image,self.rect)

我在做什么错? 我感觉在这代码中做错了什么?

    if self.moving_up and self.rect.up > 0:
        self.y += self.settings.ship_speed
    if self.moving_down and self.rect.down > self.screen_rect.down:
        self.y -= self.settings.ship_speed

解决方法

pygame.Rect对象具有虚拟属性topbottom,但没有属性updown

if self.moving_up and self.rect.top > 0:
    self.y -= self.settings.ship_speed
if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
    self.y += self.settings.ship_speed

无论如何,我建议使用pygame.Rect.clamp_ip()

返回一个新的矩形,该矩形将完全移到参数Rect内。

使用此功能,可以将对象完全保留在窗口中:

self.rect.x = self.x
self.rect.y = self.y

if self.moving_right:
    self.x += self.settings.ship_speed
if self.moving_left:
    self.x -= self.settings.ship_speed
if self.moving_up:
    self.y += self.settings.ship_speed
if self.moving_down:
    self.y -= self.settings.ship_speed

self.rect.clamp_ip(self.screen_rect)
self.x = self.rect.x
self.y = self.rect.y
,

我粘贴了以下代码:

if self.moving_up and self.rect.top > 0:
    self.y += self.settings.ship_speed
if self.moving_down and self.rect.bottom > self.screen_rect.bottom:
    self.y -= self.settings.ship_speed

但是不幸的是,它无法按需工作。 现在,当我按下UP按钮时,角色将向下移动并消失在屏幕后面。 按下DOWN按钮没有任何反应。除非角色向下移动,否则我可以使用此按钮返回到起始位置。

只有当我更改此代码后,它才起作用:

if self.moving_up and self.rect.top > 0:
    self.y -= self.settings.ship_speed
if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
    self.y += self.settings.ship_speed

对于pygame.Rect.clamp_ip()方法: 谈到未来的学习。我应该在哪里复制它,应该删除什么才能使其起作用?因为我尝试过,但是在复制代码后,角色根本没有移动。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。