鱼C论坛

 找回密码
 立即注册
查看: 36|回复: 1

[已解决]pygame模块rect问题

[复制链接]
发表于 前天 16:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import pygame as pg
from pygame.sprite import Sprite

class Animation(Sprite):
    def __init__(self,pos:tuple[int,int],filenames):
        super().__init__()
        self.images = []
        for filename in filenames:
            self.images.append(pg.image.load(filename))
        self.index = 0
        self.counter = 0
        

        self.image = self.images[self.index]
        self.rect = pg.rect.Rect(pos[0],pos[1],self.image.get_width(),self.image.get_height())
    def update(self,*Args,**kwargs):
        self.counter += 1
        if self.counter > 10:
            self.index += 1
            if self.index == len(self.images):
                self.index = 0
            self.counter = 0
        self.image = self.images[self.index]
该程序中 pg.rect.Rect(pos[0],pos[1],self.image.get_width(),self.image.get_height())是否有问题,程序可以运行,但用pg.Rect(pos[0],pos[1],self.image.get_width(),self.image.get_height())是否更可以理解
最佳答案
前天 16:23
你的代码中有几个rect相关的问题:

问题分析:

1. Rect创建参数错误:
   pg.rect.Rect(pos,pos,self.image.get_width(),self.image.get_height())
   应该传递4个参数:x, y, width, height,但你传递了pos两次(都是元组)。

2. self.image类型错误:
   self.image = self.images 将整个图片列表赋值给了self.image,但self.image应该是单个Surface对象。

修正后的代码:

  1. import pygame as pg
  2. from pygame.sprite import Sprite

  3. class Animation(Sprite):
  4.     def __init__(self, pos: tuple, filenames):
  5.         super().__init__()
  6.         self.images = []
  7.         for filename in filenames:
  8.             self.images.append(pg.image.load(filename))
  9.         self.index = 0
  10.         self.counter = 0
  11.         
  12.         # 修正1:获取当前图片
  13.         self.image = self.images[self.index]
  14.         
  15.         # 修正2:正确创建Rect
  16.         # 方法1:使用元组解包(推荐)
  17.         self.rect = pg.Rect(*pos, self.image.get_width(), self.image.get_height())
  18.         # 或者方法2:显式指定xy坐标
  19.         # self.rect = pg.Rect(pos[0], pos[1], self.image.get_width(), self.image.get_height())
  20.    
  21.     def update(self, *args, **kwargs):
  22.         self.counter += 1
  23.         if self.counter > 10:
  24.             self.index += 1
  25.             if self.index == len(self.images):
  26.                 self.index = 0
  27.             self.counter = 0
  28.             
  29.             # 修正3:更新当前显示的图片
  30.             self.image = self.images[self.index]
复制代码


解释说明:

  • pg.Rect vs pg.rect.Rect:两者完全相同,pg.Rectpg.rect.Rect 的简写,建议使用更简洁的 pg.Rect
  • Rect参数:正确格式是 pg.Rect(x, y, width, height)
        - *pos 会将元组 (x, y) 解包为两个单独参数
        - 也可以直接用 pos[0], pos[1]
  • self.image修正:必须存储单个图片(Surface),而不是整个列表,否则 get_width() 会失败。


这样就解决了rect创建的问题,并且动画也能正常切换图片了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 前天 16:23 | 显示全部楼层    本楼为最佳答案   
你的代码中有几个rect相关的问题:

问题分析:

1. Rect创建参数错误:
   pg.rect.Rect(pos,pos,self.image.get_width(),self.image.get_height())
   应该传递4个参数:x, y, width, height,但你传递了pos两次(都是元组)。

2. self.image类型错误:
   self.image = self.images 将整个图片列表赋值给了self.image,但self.image应该是单个Surface对象。

修正后的代码:

  1. import pygame as pg
  2. from pygame.sprite import Sprite

  3. class Animation(Sprite):
  4.     def __init__(self, pos: tuple, filenames):
  5.         super().__init__()
  6.         self.images = []
  7.         for filename in filenames:
  8.             self.images.append(pg.image.load(filename))
  9.         self.index = 0
  10.         self.counter = 0
  11.         
  12.         # 修正1:获取当前图片
  13.         self.image = self.images[self.index]
  14.         
  15.         # 修正2:正确创建Rect
  16.         # 方法1:使用元组解包(推荐)
  17.         self.rect = pg.Rect(*pos, self.image.get_width(), self.image.get_height())
  18.         # 或者方法2:显式指定xy坐标
  19.         # self.rect = pg.Rect(pos[0], pos[1], self.image.get_width(), self.image.get_height())
  20.    
  21.     def update(self, *args, **kwargs):
  22.         self.counter += 1
  23.         if self.counter > 10:
  24.             self.index += 1
  25.             if self.index == len(self.images):
  26.                 self.index = 0
  27.             self.counter = 0
  28.             
  29.             # 修正3:更新当前显示的图片
  30.             self.image = self.images[self.index]
复制代码


解释说明:

  • pg.Rect vs pg.rect.Rect:两者完全相同,pg.Rectpg.rect.Rect 的简写,建议使用更简洁的 pg.Rect
  • Rect参数:正确格式是 pg.Rect(x, y, width, height)
        - *pos 会将元组 (x, y) 解包为两个单独参数
        - 也可以直接用 pos[0], pos[1]
  • self.image修正:必须存储单个图片(Surface),而不是整个列表,否则 get_width() 会失败。


这样就解决了rect创建的问题,并且动画也能正常切换图片了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-8 03:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表