为了账号安全,请及时绑定邮箱和手机立即绑定

Pygame Mask 在创建为对象时不起作用?

Pygame Mask 在创建为对象时不起作用?

呼唤远方 2022-10-05 17:05:00
以下程序是一个简单的游戏,您可以在其中踢球。现在我正要为球实现一个类(“Gameobject”)以使事物面向对象。到目前为止,一切都很好。但是一旦我将mask.from_surface()作为属性添加到类中,球就不再移动了。我究竟做错了什么?工作代码(掩码位于第 61 行):import pygamefrom pygame.math import Vector2import mathwidth = 1150height = 800# ColorsWHITE = (255, 255, 255)BLACK = (0, 0, 0)GREY = (210, 210, 210)RED = (255, 0, 0)GREEN = (20, 255, 140)BLUE = (0, 0, 255)GOLD = (255, 215, 0)SILVER = (192, 192, 192)drag = 0.399  # Between 0 and 1elasticity = 0.75  # Between 0 and 1gravity = (math.pi, 0.000)pygame.init()screen = pygame.display.set_mode((width, height))clock = pygame.time.Clock()# Define game object classclass Gameobject:    """    Interactive objects with the game    """    def __init__(self, coordinates, velocity, angle, radius, objectColor):        self.x = coordinates[0]        self.y = coordinates[1]        self.velocity = velocity        self.angle = angle        self.radius = radius        self.objectColor = objectColor        self.surface = pygame.Surface((self.radius*2, self.radius*2), pygame.SRCALPHA)        self.rectangle = self.surface.get_rect(center=[self.x, self.y])    def draw(self):        """        Rendering game object        :param screen: pygame screen        """        pygame.draw.circle(self.surface, self.objectColor, [self.radius, self.radius], self.radius)# Images.BG_IMG = pygame.Surface((1150, 800))BG_IMG.fill((30, 120, 30))REDCAR_ORIGINAL = pygame.Surface((50, 30), pygame.SRCALPHA)pygame.draw.polygon(    REDCAR_ORIGINAL, (255, 0, 0), [(0, 0), (50, 10), (50, 20), (0, 30)])redcar = REDCAR_ORIGINALmyBall = Gameobject(Vector2(575, 400), Vector2(0, 0), 0, 60, GOLD)myBall.draw()mask_ball = pygame.mask.from_surface(myBall.surface)# Car variables.pos_red = Vector2(470, 370)vel_red = Vector2(3, 0)redrect = redcar.get_rect(center=pos_red)redangle = 0# Masks.mask_red = pygame.mask.from_surface(redcar)run = Truewhile run:    for event in pygame.event.get():        if event.type == pygame.QUIT:            run = False    keys = pygame.key.get_pressed()
查看完整描述

1 回答

?
开心每一天1111

TA贡献1836条经验 获得超13个赞

当您从表面创建蒙版时,表面是空的,因为圆圈是在表面上绘制的draw:


class Gameobject:

   """

   Interactive objects with the game

   """

   def __init__(self, coordinates, velocity, angle, radius, objectColor):

       # [...]


       self.surface = pygame.Surface((self.radius*2, self.radius*2), pygame.SRCALPHA)

       self.rectangle = self.surface.get_rect(center=[self.x, self.y])

       self.mask = pygame.mask.from_surface(self.surface)


   def draw(self):

       pygame.draw.circle(self.surface, self.objectColor, [self.radius, self.radius], self.radius)

.surface在的上绘制圆圈后,您必须创建蒙版Gameobject:


class Gameobject:

    # [...]    


    def draw(self):


        # draw circle on surface 

        pygame.draw.circle(self.surface, self.objectColor, [self.radius, self.radius], self.radius)


        # create mask from surface with circle

        self.mask = pygame.mask.from_surface(self.surface)


查看完整回答
反对 回复 2022-10-05
  • 1 回答
  • 0 关注
  • 126 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信