有没有办法处理draw.circle和draw.rect之间的碰撞检测?
如果我尝试使用精灵碰撞检测而不是说圆圈没有直接的特征。
我创建了两个玩家:矩形。我创造了一个球:圆圈。我想以最好的方式处理两名球员和球之间的碰撞。你能帮忙吗 ?
我在下面提供我的代码:
# DATE: 090215
TWO PLAYERS
CONTROL VIA KEYBOARD OR MOUSE
#
import pygame, sys
from pygame.constants import MOUSEMOTION, K_LEFT, K_RIGHT, K_DOWN, K_UP
pygame.init()
clock = pygame.time.Clock()
SCREENW = 800
SCREENH = 600
SCREEN = pygame.display.set_mode((SCREENW,SCREENH),0,32)
WHITE = (255,255,255)
BLACK = (0,0,0)
BLUE = (0,70,160)
RED = (255,0,0)
RECTH = 100
RECTW = 30
#players
PLAYER1P = (0,((SCREENH - RECTH)/2),RECTW,RECTH)
PLAYER2P = ((SCREENW-RECTW),((SCREENH - RECTH)/2),RECTW,RECTH)
# BALLS append with a ball for collision detection purposes
BALLS = []
# PLAYERS append with players for collision detection purposes
PLAYERS = []
# CLASS TO DEFINE PLAYER
class PlayerSprite(pygame.sprite.Sprite):
# define object atributes
def __init__(self,x,y,rwidth,rheight):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = rwidth
self.height = rheight
# define object behavior
def update(self,control,Surface,color):
key = pygame.key.get_pressed()
self.control = control
if self.control == 1:
self.y = pygame.mouse.get_pos()[1]
if self.y >= SCREENH - RECTH:
self.y = SCREENH - RECTH
if self.control == 0:
dist = 20
if key[pygame.K_DOWN]:
self.y += dist
if key[pygame.K_UP]:
self.y -= dist
if key[pygame.K_LEFT]:
None
if key[pygame.K_RIGHT]:
None
if self.y >= SCREENH - RECTH:
self.y = SCREENH - RECTH
if self.y <= 0:
self.y = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# draw player as a rectangle
# the important bit is that here you can change player shape
self.player = pygame.Rect(self.x,self.y,self.width,self.height)
pygame.draw.rect(Surface,color,self.player)
class CircleSprite(pygame.sprite.Sprite):
# define object atributes
def __init__(self,x,y,rwidth,rheight,speedx,speedy):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.speedx = speedx
self.speedy = speedy
self.width = rwidth
self.height = rheight
# define object behavior
def update(self,Surface,color):
#self.control = control
#if self.control == 1:
# self.y = pygame.mouse.get_pos()[1]
self.x += self.speedx
self.y += self.speedy
# draw player as a rectangle
# the important bit is that here you can change player shape
pygame.draw.circle(Surface, color, (self.x, self.y), self.width, 0)
# main function
def main():
# CREATE PLAYER OBJECT before WHILE loop
prec1 = PlayerSprite(0,SCREENH/2 - RECTH/2,RECTW,RECTH)
prec2 = PlayerSprite(SCREENW - RECTW,SCREENH/2 - RECTH/2,RECTW,RECTH)
prec3 = CircleSprite(SCREENW/2,SCREENH/2,RECTW/3,RECTH/3,10,5)
PLAYERS.append(prec1)
PLAYERS.append(prec2)
BALLS.append(prec3)
while True:
# update OBJECT position
SCREEN.fill(WHITE)
# 0 = KEYBOARD
# 1 = MOUSE
prec1.update(0,SCREEN,BLUE)
prec2.update(1,SCREEN,BLACK)
prec3.update(SCREEN,RED)
# check collision
is_a_collision = pygame.sprite.collide_mask(prec1,prec3)
if is_a_collision:
print "Boom1"
# change direction
if prec3.x >= SCREENW or prec3.x <= 0:
prec3.speedx = - prec3.speedx
if prec3.y >= SCREENH or prec3.y <= 0:
prec3.speedy = - prec3.speedy
pygame.display.update()
clock.tick(50)
pygame.quit()
main()