繁体   English   中英

pygame中的某些坐标后,墙壁精灵未显示在屏幕上

[英]Wall sprite not showing up on screen after certain coordinates in pygame

尝试进行碰撞检测以使精灵相互反射,但是在坐标(5,5)之后我的墙壁精灵没有显示出来,我不确定这是否可能与fill和colorkey都为白色有关,或pygame.Surface(x,y)与rect的x,y相同的事实。

这是我的墙上课:

class Wall(pygame.sprite.Sprite):

def __init__(self, color, h, d, x, y):
    super().__init__()

    self.image = pygame.Surface([x, y])
    self.image.fill(WHITE)
    self.image.set_colorkey(WHITE)

    pygame.draw.rect(self.image, color, [h, d, x, y])

    self.rect = self.image.get_rect()

这是我用于创建墙3和墙4的代码:

wall3 = Wall(BLACK, 0, 400, 700, 2)
wall_list.add(wall3)
all_sprite_list.add(wall3)

wall4 = Wall(BLACK, 700, 0, 2, 400)
wall_list.add(wall4)
all_sprite_list.add(wall4)

至于我,你有两个问题

首先:使用误导性的名称-变量x,y应该相当width, height但是稍后。

第二:假定曲面使用与屏幕相同的坐标,但事实并非如此。 它以(0,0)开始,以(x,y)结束,但是您尝试在表面(h,d)的外侧绘制rect。

所以符合

pygame.draw.rect(self.image, color, [h, d, x, y])

您需要(0,0)而不是(h,d)

pygame.draw.rect(self.image, color, [0, 0, x, y])

并且您必须将(h,d)Rect()

self.rect = self.image.get_rect()
self.rect.x = h
self.rect.y = d

坦率地说, draw.rect()将使用所有表面,因此您可以仅使用fill()来进行相同操作

def __init__(self, color, h, d, x, y):
    super().__init__()

    self.image = pygame.Surface([x, y])
    self.image.fill(color)

    self.rect = self.image.get_rect()
    self.rect.x = h
    self.rect.y = d

如果您对变量使用更好的名称,那么您将获得

def __init__(self, color, x, y, width, height):
    super().__init__()

    self.image = pygame.Surface([width, height])
    self.image.fill(color)

    self.rect = self.image.get_rect()
    self.rect.x = x
    self.rect.y = y

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM