鱼C论坛

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

pygame求解

[复制链接]
发表于 2020-9-1 20:52:55 | 显示全部楼层 |阅读模式

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

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

x
我要将星星随机地拜访在屏幕上。 以下的源代码:    运行到stars.draw(screen) 报错 AttributeError: 'Star' object has no attribute 'rect'   求解 错哪了,以及如何改。
  1. import pygame
  2. from sys import exit
  3. import random
  4. from pygame.sprite import Sprite
  5. from pygame.sprite import Group

  6. class Star(Sprite):
  7.     def __init__(self, screen):
  8.         super(Star, self).__init__()
  9.         self.image = star = pygame.image.load('images\\star.png')
  10.         self.screen = screen
  11.         self.star_rect = self.image.get_rect()
  12.         self.screen_rect = screen.get_rect()
  13.         self.star_rect.center = self.screen_rect.center

  14.     def blitme(self):
  15.         self.screen.blit(self.image, self.star_rect)

  16. def create_stars(screen, stars):
  17.     """创建星星群"""
  18.     star = Star(screen)
  19.     screen_rect = screen.get_rect()
  20.     number_x = int(screen_rect.width / (2*star.star_rect.width))
  21.     number_y = int(screen_rect.height / (2*star.star_rect.height))
  22.     random_number_y = random.randint(0, number_y)
  23.     random_number_x = random.randint(0, number_x)
  24.     for y in range(random_number_y):
  25.         for x in range(random_number_x):
  26.             star_new = Star(screen)
  27.             star_new.star_rect.x = star.star_rect.width + 2*x*star.star_rect.width
  28.             star_new.star_rect.y = star.star_rect.height + 2*y*star.star_rect.height
  29.             stars.add(star_new)

  30. def main_star():
  31.     pygame.init()
  32.     screen = pygame.display.set_mode((800, 600))
  33.     pygame.display.set_caption("星星闪耀")

  34.     stars = Group()     # 创建星星的空编组
  35.     create_stars(screen, stars)   # 将星星放入空编组中

  36.     while True:

  37.         stars.draw(screen)
  38.         pygame.display.flip()

  39.         for event in pygame.event.get():
  40.             if event.type == pygame.QUIT:
  41.                 exit()

  42. main_star()
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-9-1 21:04:06 | 显示全部楼层
已解决。。我将class Star类的时候用的是self.star_rect = self.image.get_rect()    中的star_rect全部改成rect就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-26 14:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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