【问题标题】:How to access a global object inside of a class?如何访问类内部的全局对象?
【发布时间】:2015-08-30 08:07:11
【问题描述】:

我需要访问一个类内部的全局对象来更改它的图像。代码如下:

import pygame
from livewires import games
from livewires import color
import time
import random

games.init(screen_width = 640, screen_height = 480, fps = 50)

class Cursor(games.Sprite):
    """The pokemon based cursor!"""

    def __init__(self, image, x ,y):
        super().__init__(image=image, x=x, y=y)



    def update(self):


        self.x = games.mouse.x
        self.y = games.mouse.y   
        self.check_collide()

    def check_collide(self):
        for sprite in self.overlapping_sprites:
            sprite.handle_collide()



class Play_Button(games.Sprite):
    """The 'Play' button on the menu."""

    def __init__(self , image, x ,y):
        super().__init__(image=image, x=x, y=y)

    def handle_collide(self):
        global play_obj

        play_image2 = games.load_image("playbtn2.png", transparent = True)
        play_obj.value = play_image2
        print("COME ON!")


class P1C_Button(games.Sprite):

    def __init__(self, image, x, y):
        super().__init__(image=image, x=x, y=y)

class Logo(games.Sprite):

    def handle_collide(self):
        print("Collision ignored.")



play_image = games.load_image("playbtn.png", transparent = True)
play_obj = Play_Button(image = play_image,
                x = games.screen.width/2,
                y = games.screen.height/2)
games.screen.add(play_obj)


logo_image = games.load_image("FamilyMon.png", transparent = True)
logo_obj = Logo(image = logo_image,
                x = games.screen.width/2,
                y = 75)
games.screen.add(logo_obj)

white_image = games.load_image("white.png", transparent = False)
games.screen.background = white_image

cursor_image = games.load_image("cursor.png", transparent = True)
cursor_obj = Cursor(image = cursor_image,
                    x = games.mouse.x,
                    y = games.mouse.y)
games.screen.add(cursor_obj)
games.mouse.is_visible = False
games.screen.event_grab = True

games.screen.mainloop()

其中重要的部分是在“Play_Button”的handle_collide 方法中。它试图访问创建的对象“play_obj”,但代码似乎什么也没做。当鼠标套 Play_Button 的 handle_collide 什么都没有。我已经尽力了,所以如果这似乎是一个愚蠢的问题,那就抱歉了,因为我是新人。

【问题讨论】:

  • 尝试在Play_Buttonhandle_collide 中使用print type(play_obj)。它说什么?
  • COME ON!”是否出现在sys.stdout 上,或者“无”是否意味着您没有看到打印的字符串?
  • 正在打印字符串。

标签: python pygame livewires


【解决方案1】:

你一开始就不应该使用全局变量。

您只创建一个Play_Button 对象,因此全局play_obj 总是 is self。您可以让该对象设置自己的字段:

self.value = play_image2

【讨论】:

    【解决方案2】:

    我试图理解为什么以下列方式这样做不能满足你

    def handle_collide(self):
        play_image2 = games.load_image("playbtn2.png", transparent = True)
        self.value = play_image2
        print("COME ON!")
    
    ...
    

    据我所知,只有一个Play_Button 实例,当collide 发生时,您希望更改其图像。

    那么,为什么不使用self.value

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-09
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多