如何为以后用于碰撞检测的图像创建rect?

时间:2017-06-15 13:51:40

标签: python python-3.x pygame collision-detection rect

我写了一个游戏,主角可以在鼠标点击上射击匕首,然后用上,下,左,右键移动。这个角色也跟随着鼠标。我已将恶棍添加到屏幕中,但我不确定如何为我的匕首制作一个矩形。我的意图是当匕首和小人碰撞时使用碰撞;并重新编码恶棍消失,并将其编码离开屏幕并重新出现。然而;在我能做到这一点之前,我需要为我的匕首制作一个矩形;我无论出于什么原因都找不到。

这是我定义我的rects变量和rect的代码部分:

xPlayer = 200
yPlayer = 275
dxPlayer = 0
dyPlayer = 0
playerPosition = (200,275)
daggers = []
angle = 0

villainStartx = 800
villainStarty = random.randrange(0, 550)
villainSpeed = -10
villainWidth = 100
villainHeight = 100

villainTWOStartx = 800
villainTWOStarty = random.randrange(0, 550)
villainTWOSpeed = -20
villainTWOWidth = 100
villainTWOHeight = 100

villainRect = pygame.Rect(villainStartx, villainStarty, villainWidth, villainHeight) 
villainTWORect = pygame.Rect(villainTWOStartx, villainTWOStarty, villainTWOWidth, villainTWOHeight) 
player = pygame.Rect(xPlayer, yPlayer, 160, 146)

这是我的代码部分,我在mouseclick上将匕首画到屏幕上:

filtered_daggers = []
for shot in daggers:
    if not outOfBounds(shot[0]):
        filtered_daggers.append(shot)
    daggers = filtered_daggers

    for shot in daggers:
        shot[0][0] += shot[1][0]
        shot[0][1] += shot[1][1]

    screen.blit(cloudSky, (0,0))
    screen.blit(playerRotate, playerPositionNew)

    for shot in daggers:
        x, y = shot[0]       
        screen.blit(shot[2], shot[0])

如有必要,以下是我的全部代码:

#Import the necessary modules
import pygame
import sys
import os
import math
import random

#Initialize pygame
pygame.init()

# Set the size for the surface (screen)
screenSize = (900,600)
screen = pygame.display.set_mode((screenSize),0)

# Set the caption for the screen
pygame.display.set_caption("Neverland")

#Define Colours
WHITE = (255,255,255)
BLUE = (0,0,255)
BLACK = (0,0,0)
GRAY = (128, 128, 128)
MAROON = (128, 0, 0)
NAVYBLUE = (0, 0, 128)
OLIVE = (128, 128, 0)
PURPLE = (128, 0, 128)
TEAL = (0,128,128)
PINK = (226,132,164)
MUTEDBLUE = (155,182,203)
PLUM = (221,160,221)

#Clock Setup
clock = pygame.time.Clock()

#Load Images
peterPlayer = pygame.image.load('pixelPirateOne.png')
nightBackground = pygame.image.load ('skyTwo_1.png')
daggerPlayer = pygame.image.load('daggerPlayer.png')
captHook = pygame.image.load('/pixelCaptainHook.png')
cloudSky = pygame.image.load('cloudSky.png')
pirateTwo = pygame.image.load('pixelPirateTwo.png')

#Define All Variables
xPlayer = 200
yPlayer = 275
dxPlayer = 0
dyPlayer = 0
playerPosition = (200,275)
accuracyShot = [0,0]
daggers = []
angle = 0
healthValue=194

villainStartx = 800
villainStarty = random.randrange(0, 550)
villainSpeed = -10
villainWidth = 100
villainHeight = 100

villainTWOStartx = 800
villainTWOStarty = random.randrange(0, 550)
villainTWOSpeed = -20
villainTWOWidth = 100
villainTWOHeight = 100

villainRect = pygame.Rect(villainStartx, villainStarty, villainWidth, villainHeight) 
villainTWORect = pygame.Rect(villainTWOStartx, villainTWOStarty, villainTWOWidth, villainTWOHeight) 
player = pygame.Rect(xPlayer, yPlayer, 160, 146)


#dagger = pygame.Rect(

def quitGame():
    pygame.quit()
    sys.exit()

def outOfBounds(shot):
    return shot[0] < -40 or shot[0] > 900 or shot[1] < -40 or shot[1] > 600

def villains(x,y):
    screen.blit(captHook, (x,y))

def villainsTwo(x,y):
    screen.blit(pirateTwo, (x,y))

go = True
while go:

#Quit Game
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
                quitGame()
#Move Player                
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                dxPlayer -= 25
            elif event.key == pygame.K_RIGHT:
                dxPlayer += 25
            elif event.key == pygame.K_UP:
                dyPlayer -= 25
            elif event.key == pygame.K_DOWN:
                dyPlayer += 25

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                dxPlayer = 0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                dyPlayer = 0

        elif event.type == pygame.MOUSEBUTTONDOWN:
            mousePosition = pygame.mouse.get_pos()   
            velx = math.cos(angle)*20
            vely = math.sin(angle)*20

            daggerImage = pygame.transform.rotate(daggerPlayer, -math.degrees(angle))
            width, height = daggerImage.get_size()

            daggers.append([[xPlayer,yPlayer],
                            [velx, vely], daggerImage]) 

#Update move player
    xPlayer = xPlayer + dxPlayer
    yPlayer = yPlayer + dyPlayer
    pygame.display.update()

#Learned about atan2 from --> https://docs.python.org/2/library/math.html
#Allows To Rotate Player With Mouse    
    mousePosition = pygame.mouse.get_pos()
    rise = mousePosition[1] - player.centery
    run = mousePosition[0] - player.centerx
    angle = math.atan2(rise, run)
    playerRotate = pygame.transform.rotate(peterPlayer, -math.degrees(angle))
    playerPositionNew = (xPlayer-playerRotate.get_rect().width/2, yPlayer-playerRotate.get_rect().height/2)
    player = playerRotate.get_rect(center=player.center)

#Learned about cos and sin in python from --> https://docs.python.org/2/library/math.html
#Draw daggers to screen
    filtered_daggers = []
    for shot in daggers:
        if not outOfBounds(shot[0]):
            filtered_daggers.append(shot)
        daggers = filtered_daggers

    for shot in daggers:
        shot[0][0] += shot[1][0]
        shot[0][1] += shot[1][1]

    screen.blit(cloudSky, (0,0))
    screen.blit(playerRotate, playerPositionNew)

    for shot in daggers:
        x, y = shot[0]       
        screen.blit(shot[2], shot[0])

    if villainRect.colliderect(dagger):
        print(dagger)


    pygame.display.update()
    clock.tick(30)

#Drawing the villains at random
    villains(villainStartx, villainStarty)
    villainStartx +=villainSpeed

    if villainStartx < -50:
        villainStartx = random.randrange(800,900)
        villainStarty = random.randrange(0,550)

    villainsTwo(villainTWOStartx, villainTWOStarty)
    villainTWOStartx +=villainTWOSpeed

    if villainTWOStartx < -50:
        villainTWOStartx = random.randrange(800,1000)
        villainTWOStarty = random.randrange(0,550)

#Redrawing villain after collision- To be finished after defining dagger rect

    pygame.display.update()

1 个答案:

答案 0 :(得分:2)

您可以使用匕首的图像,然后从中获取矩形属性:

dagger = pygame.image.load("dagger.png")

#you can then blit it on the screen:

screen.blit(dagger, (corrd1, corrd2))

dagger_rect = dagger.get_rect() #get the location of the dagger

if villianRect.colliderect(dagger_rect):

     #do something