Python碰撞使用pygame检测图像(图像类型:png)

问题描述 投票:-1回答:1

我想制作碰撞检测脚本。

当我运行我的脚本时,Pygame总是说图像是碰撞的。

当我使用{"rect = cat1.img.get_rect()" then "rect.colliderect(another rect of other image)}时,它会打印出“崩溃”。

我在两个精灵中使用了cat.png

问题

cat.png不好吗?

我没有使用正确的脚本吗?

我的电脑很怪异吗?

这是我的脚本和cat.png

import pygame, sys, time
from pygame.locals import *

pygame.init()

Fps = 100
fpsClock = pygame.time.Clock()

Displaysurf = pygame.display.set_mode((300, 300))# full is 1900, 1000
pygame.display.set_caption('Animation')

white = (255, 255, 255)

class cat:
      def __init__(self, x, y):
          self.img = pygame.image.load('cat.png')
          self.x = x
          self.y = y
          self.rect = self.img.get_rect()

     def draw(self):
          Displaysurf.blit(self.img, (self.x, self.y))

     def colde(self, sprite1, sprite2):
          col = pygame.sprite.collide_rect(sprite1, sprite2)
          if col == True:
               print("crash!")

cat1 = cat(10, 10)
cat2 = cat(100, 100)

while True:
    Displaysurf.fill(white)
    cat1.draw()
    cat2.draw()

    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            sys.exit()

    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        cat1.y -= 3
    if keys[pygame.K_DOWN]:
        cat1.y += 3
    if keys[pygame.K_LEFT]:
        cat1.x -= 3
    if keys[pygame.K_RIGHT]:
        cat1.x += 3
    cat1.colde(cat1, cat2)
    pygame.display.update()
    fpsClock.tick(Fps)`
python pygame png collision detection
1个回答
0
投票

你必须使用cat1.rect.xcat1.rect.x而不是cat1.xcat1.y移动物体,然后使用cat1.rect检查碰撞并绘制它

self.rect.colliderect(other_sprite)

surface.blit(self.img, self.rect)

码:

import pygame

# --- constants --- (UPPER_CASE_NAMES)

FPS = 100
WHITE = (255, 255, 255)

# --- classes --- (CamelCaseNames)

class Cat:

    def __init__(self, x, y):
        self.img = pygame.image.load('cat.png')
        self.rect = self.img.get_rect()
        self.rect.x = x
        self.rect.y = y

    def draw(self, surface):
        surface.blit(self.img, self.rect)

    def colide(self, other_sprite):
        col = self.rect.colliderect(other_sprite)
        if col:
            print("crash!")

# --- main --- (lower_case_names)

# - init -

pygame.init()

display_surf = pygame.display.set_mode((300, 300)) # full is 1900, 1000
pygame.display.set_caption('Animation')

# - objects -

cat1 = Cat(10, 10)
cat2 = Cat(100, 100)

# - mainloop -

fps_clock = pygame.time.Clock()
running = True

while running:

    # - events -

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_UP]:
        cat1.rect.y -= 3
    if keys[pygame.K_DOWN]:
        cat1.rect.y += 3
    if keys[pygame.K_LEFT]:
        cat1.rect.x -= 3
    if keys[pygame.K_RIGHT]:
        cat1.rect.x += 3

    # - updates (without draws) -

    cat1.colide(cat2)

    # - draws (without updates) -

    display_surf.fill(WHITE)
    cat1.draw(display_surf)
    cat2.draw(display_surf)

    pygame.display.update()

    fps_clock.tick(FPS)

# - end -

pygame.quit()
© www.soinside.com 2019 - 2024. All rights reserved.