如何为稍后将用于碰撞检测的图像创建矩形?



我写了一个游戏,主角可以在鼠标点击时射击dagger,并使用向上、向下、向左和向右键四处移动。角色也跟随鼠标四处走动。我已经将反派添加到屏幕中,但是我不确定如何为我的dagger制作一个矩形。我的意图是在dagger和反派碰撞时使用碰撞;并重新编码反派消失,并将其编码为屏幕并让他重新出现。然而;在我这样做之前,我需要为我的dagger做一个矩形;无论出于何种原因,我都无法发现。

这是我定义 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)

这是我的代码部分,我在鼠标单击时将dagger绘制到屏幕上:

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

您可以将图像用于dagger,然后从中获取 rect 属性:

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

相关内容

  • 没有找到相关文章

最新更新