pygame对图像进行Python碰撞检测

pygame对图像进行Python碰撞检测

本文介绍了pygame对图像进行Python碰撞检测(图像类型:png)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要制作碰撞检测脚本.

当我运行脚本 Pygame 时,总是说图像冲突.

当我使用 {"rect = cat1.img.get_rect()"然后使用"rect.colliderect(其他图像的另一个rect)}} 时,它会打印崩溃".>

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

问题

cat.png 不好吗?

我使用的脚本不正确吗?

我的计算机很奇怪吗?

这是我的脚本和 cat.png .

 导入pygame,sys,时间从pygame.locals导入*pygame.init()Fps = 100fpsClock = pygame.time.Clock()Displaysurf = pygame.display.set_mode((300,300))#full是1900,1000pygame.display.set_caption('动画')白色=(255,255,255)类猫:def __init __(self,x,y):self.img = pygame.image.load('cat.png')self.x = xself.y = yself.rect = self.img.get_rect()def draw():Displaysurf.blit(self.img,(self.x,self.y))def colde(self,sprite1,sprite2):col = pygame.sprite.collide_rect(sprite1,sprite2)如果col == True:打印(崩溃!")cat1 = cat(10,10)cat2 = cat(100,100)而True:Displaysurf.fill(白色)cat1.draw()cat2.draw()对于pygame.event.get()中的事件:如果event.type == QUIT:pygame.quit()sys.exit()键= pygame.key.get_pressed()如果键[pygame.K_UP]:cat1.y-= 3如果键[pygame.K_DOWN]:cat1.y + = 3如果键[pygame.K_LEFT]:cat1.x-= 3如果键[pygame.K_RIGHT]:cat1.x + = 3cat1.colde(cat1,cat2)pygame.display.update()fpsClock.tick(Fps)` 
解决方案

您必须使用 cat1.rect.x cat1.rect.x 而不是 cat1.x cat1.y 移动对象,然后使用 cat1.rect 检查碰撞并绘制

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

代码:

  import pygame#---常量---(UPPER_CASE_NAMES)FPS = 100白=(255,255,255)#---类---(CamelCaseNames)猫类:def __init __(self,x,y):self.img = pygame.image.load('cat.png')self.rect = self.img.get_rect()self.rect.x = xself.rect.y = ydef绘制(自身,表面):surface.blit(self.img,self.rect)def colide(自己,other_sprite):col = self.rect.colliderect(other_sprite)如果col:打印(崩溃!")#---主要---(lower_case_names)# - 在里面 -pygame.init()display_surf = pygame.display.set_mode((300,300))#full是1900,1000pygame.display.set_caption('动画')#-对象-cat1 = Cat(10,10)cat2 = Cat(100,100)#-主循环-fps_clock = pygame.time.Clock()运行=真在跑步的时候:#-事件-对于pygame.event.get()中的事件:如果event.type == pygame.QUIT:运行=假键= pygame.key.get_pressed()如果键[pygame.K_UP]:cat1.rect.y-= 3如果键[pygame.K_DOWN]:cat1.rect.y + = 3如果键[pygame.K_LEFT]:cat1.rect.x-= 3如果键[pygame.K_RIGHT]:cat1.rect.x + = 3#-更新(不抽签)-cat1.colide(cat2)#-抽签(无更新)-display_surf.fill(白色)cat1.draw(display_surf)cat2.draw(display_surf)pygame.display.update()fps_clock.tick(帧/秒)# - 结尾 -pygame.quit() 

I want to make the Collision Detection scripts.

When I run my scripts Pygame always say the images are in collision.

It prints "crash" when I use {"rect = cat1.img.get_rect()" then "rect.colliderect(another rect of other image)} and this way both.

I used cat.png in two sprites.

Questions

Is cat.png bad?

Am I not using the right scripts?

Is my computer weird?

Here are my scripts and the 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)`
解决方案

You have to use cat1.rect.x and cat1.rect.x instead of cat1.x and cat1.y to move object and then use cat1.rect to check collision and to draw it

self.rect.colliderect(other_sprite)

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

Code:

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

这篇关于pygame对图像进行Python碰撞检测(图像类型:png)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 11:25