如何在不使用矩形的情况下添加点击碰撞的精灵?
我刚接触pygame,但对python还算熟悉。我正在尝试制作一个外星人射击游戏,想要实现十字准星和游戏中的角色(精灵)之间的碰撞。我想用一个圆形去碰撞外星人的图像和形状?我不想用矩形,只想要“点击精灵就消失”。有没有什么办法可以做到这一点?以下是我的代码:
"""
Alien_Shooter
Description:
a game to shoot aliens
"""
import pygame
import tsk
import random
pygame.init()
WIDTH = 1018
HEIGHT = 573
w = pygame.display.set_mode([WIDTH, HEIGHT])
c = pygame.time.Clock()
shooting= False
invaderImage = []
invader_X = []
invader_Y = []
invader_Xchange = []
invader_Ychange = []
no_of_invaders = 8
turn = False
for num in range(no_of_invaders):
alien = pygame.image.load('SquidMan.png')
alien = pygame.transform.scale(alien, (100,100))
alien_hb = pygame.Rect(200, 500, 50, 50)
#collide = pygame.Rect.colliderect(player_rect, mouse)
invaderImage.append(alien)
invader_X.append(random.randint(64, 737))
invader_Y.append(random.randint(30, 180))
invader_Xchange.append(0)
invader_Ychange.append(1)
def invader(x, y, i):
w.blit(invaderImage[i], (x, y))
drawing = True
while drawing:
for e in pygame.event.get():
if e.type == pygame.QUIT:
drawing = False
if e.type == pygame.MOUSEBUTTONDOWN:
shooting = True
else:
shooting = False
#collide = pygame.Rect.colliderect(alien_rect, center_rect)
x, y = pygame.mouse.get_pos()
#print(x,y)
if shooting == True:
# gives the flashing effect when shooting
color = (0,0,0)
else:
color = (255,255,255)
for i in range(no_of_invaders):
invader_X[i] += invader_Xchange[i]
# movement of the invader
for i in range(no_of_invaders):
invader_Y[i] += invader_Ychange[i]
invader(invader_X[i], invader_Y[i], i)
line1 = pygame.draw.line(w, color, (x,y), (350, 700), 3)
line2 = pygame.draw.line(w, color, (x,y), (650, 700), 3)
# crosshares
center = pygame.draw.circle(w,color, (x, y), 10, 2)
pygame.draw.line(w, color, (x,y), (x+20, y), 3)
pygame.draw.line(w, color, (x,y), (x-20, y), 3)
####################
# | #
# --o-- #
# | #
####################
pygame.draw.line(w, color, (x,y), (x, y+20), 3)
pygame.draw.line(w, color, (x,y), (x, y-20), 3)
pygame.display.flip()
c.tick(20)
w.fill((0,0,0))
我试着修复这个问题,但不太确定怎么在不使用矩形的情况下做到。
1 个回答
-1
你可以通过一个简单的数学公式来计算鼠标点击位置(mouse_x, mouse_y)和精灵中心(alien_center_x, alien_center_y)之间的距离。这个公式叫做勾股定理,计算方式是:距离 = sqrt((alien_center_x - mouse_x)**2 + (alien_center_y - mouse_y)**2)。如果这个距离小于一个特定的值,比如精灵宽度的一半(如果精灵大致是圆形的,这个值就相当于它的半径),那么就可以认为你点击到了精灵。
下面是一段代码来说明这个方法:
import pygame
import math
pygame.init()
screen = pygame.display.set_mode((800, 600))
# Load your alien sprite here
alien = pygame.image.load('alien.png').convert_alpha()
alien_rect = alien.get_rect(center=(400, 300))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
distance = math.sqrt((alien_rect.centerx - mouse_pos[0]) ** 2 +
(alien_rect.centery - mouse_pos[1]) ** 2)
if distance < 50: # Adjust this value to fit your sprite size
print("Hit!")
screen.fill((0, 0, 0))
screen.blit(alien, alien_rect)
pygame.display.flip()
只需要把'alien.png'替换成你的精灵图片,并根据需要调整点击检测的半径就可以了。