鱼C论坛

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

[已解决]group问题

[复制链接]
发表于 2020-7-18 09:03:11 | 显示全部楼层 |阅读模式

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

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

x
import pygame
import sys
from pygame.locals import *

class Brick:
    def __init__(self,position):
        self.image=pygame.image.load('./material/brick.png').convert_alpha()
        self.position=position
        self.image_rect=self.image.get_rect()
        self.image_rect.left,self.image_rect.top=self.position[0],self.position[1]
        


def main():
    pygame.init()
    size=width,height=800,600
    screen=pygame.display.set_mode(size)
    bg_c=(255,255,255)

    ball_image=pygame.image.load('./material/rainbowball.png').convert_alpha()
    clock=pygame.time.Clock()
    group=pygame.sprite.Group()
    running=True
    bricks=[]
    while running:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                sys.exit()
               

        for i in range(10):
            for j in range(3):
                brick=Brick((80*i,40*j))
                bricks.append(brick)
                group.add(brick)

        screen.fill(bg_c)
        
        for i in bricks:
            screen.blit(i.image,i.image_rect)

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


if __name__=='__main__':
    main()

想做个打砖块,结果一开始就报错了,求解

捕获.PNG
最佳答案
2020-7-18 09:08:27
Brick类的定义改成这样:
class Brick(pygame.sprite.Sprite):
    def __init__(self,position):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load('./material/brick.png').convert_alpha()
        self.position=position
        self.image_rect=self.image.get_rect()
        self.image_rect.left,self.image_rect.top=self.position[0],self.position[1]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-7-18 09:08:27 | 显示全部楼层    本楼为最佳答案   
Brick类的定义改成这样:
class Brick(pygame.sprite.Sprite):
    def __init__(self,position):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load('./material/brick.png').convert_alpha()
        self.position=position
        self.image_rect=self.image.get_rect()
        self.image_rect.left,self.image_rect.top=self.position[0],self.position[1]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-8 21:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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