鱼C论坛

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

[已解决]P89 问题2

[复制链接]
发表于 2020-4-27 14:55:21 | 显示全部楼层 |阅读模式

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

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

x
import pygame
import sys
import math
from pygame.locals import *
from random import *

#球类继承自Sprirte类
class Ball(pygame.sprite.Sprite):
    def __init__(self,glayball_image,greenball_image,position,speed,bg_size,target):
        #初始化动作精灵
        pygame.sprite.Sprite.__init__(self)

        self.glayball_image = pygame.image.load(glayball_image).convert_alpha()
        self.greenyball_image = pygame.image.load(greenball_image).convert_alpha()
        self.rect = self.glayball_image.get_rect()
        #将小球放在指定位置
        self.rect.left,self.rect.top = position
        self.speed = speed
        self.target = target
        self.control = False
        self.width,self.height = bg_size[0],bg_size[1]
        self.radius = self.rect.width /2

    def move(self):
        self.rect = self.rect.move(self.speed)

        #如果小球的左侧出了边界,那么将小球左侧改为右侧的边界
        #这样便实现了左边进入,右边出来的效果
        if self.rect.right < 0:
            self.rect.left = self.width

        elif self.rect.left > self.width:
            self.rect.right = 0

        elif self.rect.bottom <0:
            self.rect.top = self.height

        elif self.rect.top > self.height:
            self.rect.bottom = 0

    def check(self,motion):
        if self.target < motion <self.target +5:
            return True
        else:
            return False
        

class Glass(pygame.sprite.Sprite):
    def __init__(self,glass_image,mouse_image,bg_size):
        #初始化动作精灵
        pygame.sprite.Sprite.__init__(self)

        self.glass_image = pygame.image.load(glass_image).convert_alpha()
        self.glass_rect = self.glass_image.get_rect()
        self.glass_rect.left,self.glass_rect.top = \
                             (bg_size[0] - self.glass_rect.width)//2,\
                             bg_size[1]- self.glass_rect.height

        self.mouse_image = pygame.image.load(mouse_image).convert_alpha()
        self.mouse_rect = self.mouse_image.get_rect()
        self.mouse_rect.left,self.mouse_rect.top=\
                                                   self.glass_rect.left,self.glass_rect.top
        pygame.mouse.set_visible(False)
   


def main():
    pygame.init()

    glayball_image = 'ball.png'
    greenball_image='greenball.png'
    glass_image = 'boli.png'
    mouse_image = 'sz.png'
    bg_image = 'bg2.png'

    running = True

    #添加背景音乐
    pygame.mixer.music.load('1.mp3')
    pygame.mixer.music.play()

    #添加音效
    loser_sound = pygame.mixer.Sound('1.wav')
    laugh_sound = pygame.mixer.Sound('2.wav')
    winner_sound = pygame.mixer.Sound('3.wav')
    hole_sound = pygame.mixer.Sound('4.wav')

    #音乐播放完游戏结束
    GAMEOVER = USEREVENT
    pygame.mixer.music.set_endevent(GAMEOVER)

    #根据背景图片指定游戏界面尺寸
    bg_size = width,height = 1024,576
    screen = pygame.display.set_mode(bg_size)
    pygame.display.set_caption('Play the ball - FishC Demo')

    background = pygame.image.load(bg_image).convert_alpha()

    #用来存放小球对象的列表
    balls=[]
    group = pygame.sprite.Group()

    #创建五个小球
    for i in range(5):
        #位置随机,速度随机
        position = randint(0,width-100),randint(0,height-100)
        speed = [randint(-10,10),randint(-10,10)]
        ball = Ball(glayball_image,greenball_image,position,speed,bg_size,5*(i+1))
        while pygame.sprite.spritecollide(ball,group,False,pygame.sprite.collide_circle):
            ball.rect.left,ball.rect.top = randint(0,width-100),randint(0,height-100)
            
        balls.append(ball)
        group.add(ball)

    glass = Glass(glass_image,mouse_image,bg_size)

    motion = 0

    MYTIMER = USEREVENT +1
    pygame.time.set_timer(MYTIMER,1000)   

    clock = pygame.time.Clock()

    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()

            elif event.type == GAMEOVER:
                loser_sound.play()
                pygame.time.delay(2000)
                laugh_sound.play()
                running = False

            elif event.type == MYTIMER:
                if motion:
                    for each in group:
                        if each.check(motion):
                            each.speed = [0,0]
                            each.control = True
                    motion = 0
            elif event.type == MOUSEMOTION:
                motion += 1
               
                           

        screen.blit(background,(0,0))
        screen.blit(glass.glass_image,glass.glass_rect)

        glass.mouse_rect.left,glass.mouse_rect.top = pygame.mouse.get_pos()
        if glass.mouse_rect.left < glass.glass_rect.left:
            glass.mouse_rect.left = glass.glass_rect.left
        if glass.mouse_rect.left > glass.glass_rect.right - glass.mouse_rect.width:
            glass.mouse_rect.left = glass.glass_rect.right - glass.mouse_rect.width
        if glass.mouse_rect.top < glass.glass_rect.top:
            glass.mouse_rect.top = glass.glass_rect.top
        if glass.mouse_rect.top > glass.glass_rect.bottom- glass.mouse_rect.height:
            glass.mouse_rect.top = glass.glass_rect.bottom- glass.mouse_rect.height

        screen.blit(glass.mouse_image,glass.mouse_rect)

        for each in balls:
            each.move()
            if each.control:
                screen.blit(each.greenball_image,each.rect)
            else:
                screen.blit(each.glayball_image,each.rect)


        for each in group:
            group.remove(each)

            if pygame.sprite.spritecollide(each,group,False,pygame.sprite.collide_circle):
                each.speed[0] = -each.speed[0]
                each.speed[1] = -each.speed[1]

            group.add(each)

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

if __name__ == '__main__':
    main()

   


Traceback (most recent call last):
  File "C:/Users/john/Desktop/python练习/p89.py", line 183, in <module>
    main()
  File "C:/Users/john/Desktop/python练习/p89.py", line 165, in main
    screen.blit(each.greenball_image,each.rect)
AttributeError: 'Ball' object has no attribute 'greenball_image'
最佳答案
2020-4-27 14:58:58
  1.         self.greenyball_image = pygame.image.load(greenball_image).convert_alpha()
复制代码

这里你自己打成了greenyball,把y去掉就行啦
修改后代码:

  1. import pygame
  2. import sys
  3. import math
  4. from pygame.locals import *
  5. from random import *

  6. #球类继承自Sprirte类
  7. class Ball(pygame.sprite.Sprite):
  8.     def __init__(self,glayball_image,greenball_image,position,speed,bg_size,target):
  9.         #初始化动作精灵
  10.         pygame.sprite.Sprite.__init__(self)

  11.         self.glayball_image = pygame.image.load(glayball_image).convert_alpha()
  12.         self.greenball_image = pygame.image.load(greenball_image).convert_alpha()
  13.         self.rect = self.glayball_image.get_rect()
  14.         #将小球放在指定位置
  15.         self.rect.left,self.rect.top = position
  16.         self.speed = speed
  17.         self.target = target
  18.         self.control = False
  19.         self.width,self.height = bg_size[0],bg_size[1]
  20.         self.radius = self.rect.width /2

  21.     def move(self):
  22.         self.rect = self.rect.move(self.speed)

  23.         #如果小球的左侧出了边界,那么将小球左侧改为右侧的边界
  24.         #这样便实现了左边进入,右边出来的效果
  25.         if self.rect.right < 0:
  26.             self.rect.left = self.width

  27.         elif self.rect.left > self.width:
  28.             self.rect.right = 0

  29.         elif self.rect.bottom <0:
  30.             self.rect.top = self.height

  31.         elif self.rect.top > self.height:
  32.             self.rect.bottom = 0

  33.     def check(self,motion):
  34.         if self.target < motion <self.target +5:
  35.             return True
  36.         else:
  37.             return False
  38.         

  39. class Glass(pygame.sprite.Sprite):
  40.     def __init__(self,glass_image,mouse_image,bg_size):
  41.         #初始化动作精灵
  42.         pygame.sprite.Sprite.__init__(self)

  43.         self.glass_image = pygame.image.load(glass_image).convert_alpha()
  44.         self.glass_rect = self.glass_image.get_rect()
  45.         self.glass_rect.left,self.glass_rect.top = \
  46.                              (bg_size[0] - self.glass_rect.width)//2,\
  47.                              bg_size[1]- self.glass_rect.height

  48.         self.mouse_image = pygame.image.load(mouse_image).convert_alpha()
  49.         self.mouse_rect = self.mouse_image.get_rect()
  50.         self.mouse_rect.left,self.mouse_rect.top=\
  51.                                                    self.glass_rect.left,self.glass_rect.top
  52.         pygame.mouse.set_visible(False)
  53.    


  54. def main():
  55.     pygame.init()

  56.     glayball_image = 'ball.png'
  57.     greenball_image='greenball.png'
  58.     glass_image = 'boli.png'
  59.     mouse_image = 'sz.png'
  60.     bg_image = 'bg2.png'

  61.     running = True

  62.     #添加背景音乐
  63.     pygame.mixer.music.load('1.mp3')
  64.     pygame.mixer.music.play()

  65.     #添加音效
  66.     loser_sound = pygame.mixer.Sound('1.wav')
  67.     laugh_sound = pygame.mixer.Sound('2.wav')
  68.     winner_sound = pygame.mixer.Sound('3.wav')
  69.     hole_sound = pygame.mixer.Sound('4.wav')

  70.     #音乐播放完游戏结束
  71.     GAMEOVER = USEREVENT
  72.     pygame.mixer.music.set_endevent(GAMEOVER)

  73.     #根据背景图片指定游戏界面尺寸
  74.     bg_size = width,height = 1024,576
  75.     screen = pygame.display.set_mode(bg_size)
  76.     pygame.display.set_caption('Play the ball - FishC Demo')

  77.     background = pygame.image.load(bg_image).convert_alpha()

  78.     #用来存放小球对象的列表
  79.     balls=[]
  80.     group = pygame.sprite.Group()

  81.     #创建五个小球
  82.     for i in range(5):
  83.         #位置随机,速度随机
  84.         position = randint(0,width-100),randint(0,height-100)
  85.         speed = [randint(-10,10),randint(-10,10)]
  86.         ball = Ball(glayball_image,greenball_image,position,speed,bg_size,5*(i+1))
  87.         while pygame.sprite.spritecollide(ball,group,False,pygame.sprite.collide_circle):
  88.             ball.rect.left,ball.rect.top = randint(0,width-100),randint(0,height-100)
  89.             
  90.         balls.append(ball)
  91.         group.add(ball)

  92.     glass = Glass(glass_image,mouse_image,bg_size)

  93.     motion = 0

  94.     MYTIMER = USEREVENT +1
  95.     pygame.time.set_timer(MYTIMER,1000)   

  96.     clock = pygame.time.Clock()

  97.     while running:
  98.         for event in pygame.event.get():
  99.             if event.type == QUIT:
  100.                 sys.exit()

  101.             elif event.type == GAMEOVER:
  102.                 loser_sound.play()
  103.                 pygame.time.delay(2000)
  104.                 laugh_sound.play()
  105.                 running = False

  106.             elif event.type == MYTIMER:
  107.                 if motion:
  108.                     for each in group:
  109.                         if each.check(motion):
  110.                             each.speed = [0,0]
  111.                             each.control = True
  112.                     motion = 0
  113.             elif event.type == MOUSEMOTION:
  114.                 motion += 1
  115.                
  116.                            

  117.         screen.blit(background,(0,0))
  118.         screen.blit(glass.glass_image,glass.glass_rect)

  119.         glass.mouse_rect.left,glass.mouse_rect.top = pygame.mouse.get_pos()
  120.         if glass.mouse_rect.left < glass.glass_rect.left:
  121.             glass.mouse_rect.left = glass.glass_rect.left
  122.         if glass.mouse_rect.left > glass.glass_rect.right - glass.mouse_rect.width:
  123.             glass.mouse_rect.left = glass.glass_rect.right - glass.mouse_rect.width
  124.         if glass.mouse_rect.top < glass.glass_rect.top:
  125.             glass.mouse_rect.top = glass.glass_rect.top
  126.         if glass.mouse_rect.top > glass.glass_rect.bottom- glass.mouse_rect.height:
  127.             glass.mouse_rect.top = glass.glass_rect.bottom- glass.mouse_rect.height

  128.         screen.blit(glass.mouse_image,glass.mouse_rect)

  129.         for each in balls:
  130.             each.move()
  131.             if each.control:
  132.                 screen.blit(each.greenball_image,each.rect)
  133.             else:
  134.                 screen.blit(each.glayball_image,each.rect)


  135.         for each in group:
  136.             group.remove(each)

  137.             if pygame.sprite.spritecollide(each,group,False,pygame.sprite.collide_circle):
  138.                 each.speed[0] = -each.speed[0]
  139.                 each.speed[1] = -each.speed[1]

  140.             group.add(each)

  141.         pygame.display.flip()
  142.         clock.tick(30)

  143. if __name__ == '__main__':
  144.     main()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-27 14:58:58 | 显示全部楼层    本楼为最佳答案   
  1.         self.greenyball_image = pygame.image.load(greenball_image).convert_alpha()
复制代码

这里你自己打成了greenyball,把y去掉就行啦
修改后代码:

  1. import pygame
  2. import sys
  3. import math
  4. from pygame.locals import *
  5. from random import *

  6. #球类继承自Sprirte类
  7. class Ball(pygame.sprite.Sprite):
  8.     def __init__(self,glayball_image,greenball_image,position,speed,bg_size,target):
  9.         #初始化动作精灵
  10.         pygame.sprite.Sprite.__init__(self)

  11.         self.glayball_image = pygame.image.load(glayball_image).convert_alpha()
  12.         self.greenball_image = pygame.image.load(greenball_image).convert_alpha()
  13.         self.rect = self.glayball_image.get_rect()
  14.         #将小球放在指定位置
  15.         self.rect.left,self.rect.top = position
  16.         self.speed = speed
  17.         self.target = target
  18.         self.control = False
  19.         self.width,self.height = bg_size[0],bg_size[1]
  20.         self.radius = self.rect.width /2

  21.     def move(self):
  22.         self.rect = self.rect.move(self.speed)

  23.         #如果小球的左侧出了边界,那么将小球左侧改为右侧的边界
  24.         #这样便实现了左边进入,右边出来的效果
  25.         if self.rect.right < 0:
  26.             self.rect.left = self.width

  27.         elif self.rect.left > self.width:
  28.             self.rect.right = 0

  29.         elif self.rect.bottom <0:
  30.             self.rect.top = self.height

  31.         elif self.rect.top > self.height:
  32.             self.rect.bottom = 0

  33.     def check(self,motion):
  34.         if self.target < motion <self.target +5:
  35.             return True
  36.         else:
  37.             return False
  38.         

  39. class Glass(pygame.sprite.Sprite):
  40.     def __init__(self,glass_image,mouse_image,bg_size):
  41.         #初始化动作精灵
  42.         pygame.sprite.Sprite.__init__(self)

  43.         self.glass_image = pygame.image.load(glass_image).convert_alpha()
  44.         self.glass_rect = self.glass_image.get_rect()
  45.         self.glass_rect.left,self.glass_rect.top = \
  46.                              (bg_size[0] - self.glass_rect.width)//2,\
  47.                              bg_size[1]- self.glass_rect.height

  48.         self.mouse_image = pygame.image.load(mouse_image).convert_alpha()
  49.         self.mouse_rect = self.mouse_image.get_rect()
  50.         self.mouse_rect.left,self.mouse_rect.top=\
  51.                                                    self.glass_rect.left,self.glass_rect.top
  52.         pygame.mouse.set_visible(False)
  53.    


  54. def main():
  55.     pygame.init()

  56.     glayball_image = 'ball.png'
  57.     greenball_image='greenball.png'
  58.     glass_image = 'boli.png'
  59.     mouse_image = 'sz.png'
  60.     bg_image = 'bg2.png'

  61.     running = True

  62.     #添加背景音乐
  63.     pygame.mixer.music.load('1.mp3')
  64.     pygame.mixer.music.play()

  65.     #添加音效
  66.     loser_sound = pygame.mixer.Sound('1.wav')
  67.     laugh_sound = pygame.mixer.Sound('2.wav')
  68.     winner_sound = pygame.mixer.Sound('3.wav')
  69.     hole_sound = pygame.mixer.Sound('4.wav')

  70.     #音乐播放完游戏结束
  71.     GAMEOVER = USEREVENT
  72.     pygame.mixer.music.set_endevent(GAMEOVER)

  73.     #根据背景图片指定游戏界面尺寸
  74.     bg_size = width,height = 1024,576
  75.     screen = pygame.display.set_mode(bg_size)
  76.     pygame.display.set_caption('Play the ball - FishC Demo')

  77.     background = pygame.image.load(bg_image).convert_alpha()

  78.     #用来存放小球对象的列表
  79.     balls=[]
  80.     group = pygame.sprite.Group()

  81.     #创建五个小球
  82.     for i in range(5):
  83.         #位置随机,速度随机
  84.         position = randint(0,width-100),randint(0,height-100)
  85.         speed = [randint(-10,10),randint(-10,10)]
  86.         ball = Ball(glayball_image,greenball_image,position,speed,bg_size,5*(i+1))
  87.         while pygame.sprite.spritecollide(ball,group,False,pygame.sprite.collide_circle):
  88.             ball.rect.left,ball.rect.top = randint(0,width-100),randint(0,height-100)
  89.             
  90.         balls.append(ball)
  91.         group.add(ball)

  92.     glass = Glass(glass_image,mouse_image,bg_size)

  93.     motion = 0

  94.     MYTIMER = USEREVENT +1
  95.     pygame.time.set_timer(MYTIMER,1000)   

  96.     clock = pygame.time.Clock()

  97.     while running:
  98.         for event in pygame.event.get():
  99.             if event.type == QUIT:
  100.                 sys.exit()

  101.             elif event.type == GAMEOVER:
  102.                 loser_sound.play()
  103.                 pygame.time.delay(2000)
  104.                 laugh_sound.play()
  105.                 running = False

  106.             elif event.type == MYTIMER:
  107.                 if motion:
  108.                     for each in group:
  109.                         if each.check(motion):
  110.                             each.speed = [0,0]
  111.                             each.control = True
  112.                     motion = 0
  113.             elif event.type == MOUSEMOTION:
  114.                 motion += 1
  115.                
  116.                            

  117.         screen.blit(background,(0,0))
  118.         screen.blit(glass.glass_image,glass.glass_rect)

  119.         glass.mouse_rect.left,glass.mouse_rect.top = pygame.mouse.get_pos()
  120.         if glass.mouse_rect.left < glass.glass_rect.left:
  121.             glass.mouse_rect.left = glass.glass_rect.left
  122.         if glass.mouse_rect.left > glass.glass_rect.right - glass.mouse_rect.width:
  123.             glass.mouse_rect.left = glass.glass_rect.right - glass.mouse_rect.width
  124.         if glass.mouse_rect.top < glass.glass_rect.top:
  125.             glass.mouse_rect.top = glass.glass_rect.top
  126.         if glass.mouse_rect.top > glass.glass_rect.bottom- glass.mouse_rect.height:
  127.             glass.mouse_rect.top = glass.glass_rect.bottom- glass.mouse_rect.height

  128.         screen.blit(glass.mouse_image,glass.mouse_rect)

  129.         for each in balls:
  130.             each.move()
  131.             if each.control:
  132.                 screen.blit(each.greenball_image,each.rect)
  133.             else:
  134.                 screen.blit(each.glayball_image,each.rect)


  135.         for each in group:
  136.             group.remove(each)

  137.             if pygame.sprite.spritecollide(each,group,False,pygame.sprite.collide_circle):
  138.                 each.speed[0] = -each.speed[0]
  139.                 each.speed[1] = -each.speed[1]

  140.             group.add(each)

  141.         pygame.display.flip()
  142.         clock.tick(30)

  143. if __name__ == '__main__':
  144.     main()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-19 13:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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