Python 图像与 pygame 的碰撞检测(图像类型:png)
Python Collision Detection of image with pygame (image type : png)
我想制作碰撞检测脚本。
当我 运行 我的脚本 Pygame
总是说图像发生冲突。
当我使用 {"rect = cat1.img.get_rect()" then "rect.colliderect(another rect of other image)}
时它打印 "crash" 并且这两种方式都如此。
我在两个精灵中使用了 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)`
你必须使用 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
# --- 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
总是说图像发生冲突。
当我使用 {"rect = cat1.img.get_rect()" then "rect.colliderect(another rect of other image)}
时它打印 "crash" 并且这两种方式都如此。
我在两个精灵中使用了 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)`
你必须使用 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
# --- 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()