1
私はウイルスと長方形の衝突を検出しようとしていますが、クラスごとにクラスを設定していますが、衝突のコードがありますが、コードが正常に機能せず、何が間違っているのか分かりません。私は、画像を長方形に変え、ウイルスとベースとの衝突を検出するために衝突検出を使用しましたが、何も起こりません。これがなぜなのか、どんな助けにも感謝します!Pygameの画像と矩形の衝突検出?
import pygame, random, time
pygame.init()
#Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
#Screen Stuff
size = (1080, 640)
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Random Platformer')
icon = pygame.image.load('Icon.jpg')
pygame.display.set_icon(icon)
pygame.display.update()
#Misc Stuff
clock = pygame.time.Clock()
running = True
#Objects
class Player:
def __init__(self, x, y, image):
self.x = x
self.y = y
self.player = pygame.image.load(image)
self.rect = self.player.get_rect()
def load(self):
screen.blit(self.player, (self.x, self.y))
def move_right(self):
self.x += 7.5
def move_left(self):
self.x -= 7.5
def move_up(self):
self.y -= 7.5
def move_down(self):
self.y += 7.5
class Block:
def __init__(self, x, y, width, length, edge_thickness):
self.x = x
self.y = y
self.width = width
self.length = length
self.edge_thickness = edge_thickness
def load(self):
self.rect = pygame.draw.rect(screen, BLACK, [self.x, self.y,
self.width, self.length], self.edge_thickness)
#Players
virus = Player(100, 539, 'Virus.jpg')
#Blocks
level_base = Block(0, 561, 1080, 80, 0)
#Game Loop
while running:
#Level Generation
screen.fill(WHITE)
level_base.load()
virus.load()
if level_base.rect.colliderect(virus.rect):
print ('Hi')
#Controls
key_press = pygame.key.get_pressed()
if key_press[pygame.K_d]:
virus.move_right()
if key_press[pygame.K_a]:
virus.move_left()
if key_press[pygame.K_w]:
virus.move_up()
if key_press[pygame.K_s]:
virus.move_down()
#Game Code
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
break
pygame.display.flip()
clock.tick(60)
サイドノート:[この回答](https://stackoverflow.com/a/47883299/6220679)で説明されているように画像を読み込むことをおすすめします。 – skrx
あなたは大歓迎です! – skrx