简体   繁体   English

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

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

Trying to make collision detection as means to make sprites bounce off one another, but my wall sprites aren't showing up after coords (5, 5) I wasn't sure if maybe it had to do with fill and colorkey both being white, or the fact that pygame.Surface(x, y) is the same as the x, y for the rect. 尝试进行碰撞检测以使精灵相互反射,但是在坐标(5,5)之后我的墙壁精灵没有显示出来,我不确定这是否可能与fill和colorkey都为白色有关,或pygame.Surface(x,y)与rect的x,y相同的事实。

Here's my wall class: 这是我的墙上课:

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()

and here's my code for my call to create wall 3 an wall 4: 这是我用于创建墙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)

As for me you have two problems 至于我,你有两个问题

First: you use missleading names - variables x,y should be rather width, height but this later. 首先:使用误导性的名称-变量x,y应该相当width, height但是稍后。

Second: you assume that surface uses the same coordinates as screen but it not true. 第二:假定曲面使用与屏幕相同的坐标,但事实并非如此。 It starts at (0,0) and ends in your (x,y) but you try to draw rect in position (h,d) which is outside surface. 它以(0,0)开始,以(x,y)结束,但是您尝试在表面(h,d)的外侧绘制rect。

So in line 所以符合

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

you need (0,0) instead of (h,d) 您需要(0,0)而不是(h,d)

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

and you have to use (h,d) with Rect() 并且您必须将(h,d)Rect()

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

Frankly, draw.rect() will use all surface so you could do the same using only fill() 坦率地说, 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

If you use better names for variables then you get 如果您对变量使用更好的名称,那么您将获得

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