[英]How do I change the hitbox of one of my sprites?
因此,當游戲開始時,會有一些盒子供用戶躲避,但目前,物體的碰撞盒有點偏離……例如,當您將船移過一個盒子時,它會注冊為命中,游戲結束.
這是針對軟件設計和開發 HSC 評估任務的,我不太確定如何解決此問題
這是代碼!
#This program was created by Tadiwa Mooyo
#more of the car game has been worked on, now there are boxes for the user to "dodge" and it will display the crash message when the boxes are hit
#This program was started on the 22/02/2019
import pygame
import time
import random
pygame.init()
display_width = 1200
display_height = 700
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
car_width = 100
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('The Great Space Escape!')
clock = pygame.time.Clock()
carImg = pygame.image.load('Ships_directory\\Green_black_ship.png')
def things_dodged(count):
font = pygame.font.SysFont(None, 25)
text = font.render("Score: "+str(count), True, white)
gameDisplay.blit(text,(0,0))
def things(thingx, thingy, thingw, thingh, color):
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
def car(x,y):
gameDisplay.blit(carImg,(x,y))
def text_objects(text, font):
textSurface = font.render(text, True, white)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
message_display("You Crashed")
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(black)
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects("A bit Racey", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
clock.tick(15)
def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.5)
x_change = 0
thing_startx = random.randrange(0, display_width)
thing_starty = -600
thing_speed = 4
thing_width = 200
thing_height = 200
dodged = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -10
if event.key ==pygame.K_RIGHT:
x_change = 10
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(black)
#things(thingx, thingy, thingw, thingh, color)
things(thing_startx, thing_starty, thing_width, thing_height, white)
thing_starty += thing_speed
car(x,y)
things_dodged(dodged)
if x > display_width - car_width or x < 0:
crash()
if thing_starty > display_height:
thing_starty = 0 - thing_height
thing_startx = random.randrange(0,display_width)
dodged += 1
thing_speed += 0.5
thing_width += (dodged * 1.4)
if y < thing_starty+thing_height:
print('y crossover')
if x > thing_startx and x < thing_startx + thing_width or x+car_width > thing_startx and x + car_width < thing_startx+thing_width:
print('x crossover')
crash()
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
使用pygame.Rect
來簡化你的代碼並通過.colliderect()
實現碰撞測試:
例如
things_rect = pygame.Rect(thing_startx, thing_starty, thing_width, thing_height)
car_rect = pygame.Rect(x, y, *carImg.get_size())
if car_rect.colliderect(things_rect):
crash()
如果你想實現你的“自己的”測試,它檢查 2 個矩形是否相交,你必須檢查矩形在兩個維度上是否“重疊”。
如果x1 < x2+w2 and x2 < x1+w1
,則 2 個范圍 [ x1
, x1+w1
] 和 [ x2
, x2+w2
] 重疊。
因此,可以按如下方式實現矩形的相交測試:
car_w, car_h = carImg.get_size()
if (thing_startx < x+car_w and x < thing_startx+thing_width and
thing_starty < y+car_h and y < thing_starty+thing_height):
crash()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.