我如何在HitBox中移动图像? pygame
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我如何在HitBox中移动图像? pygame相关的知识,希望对你有一定的参考价值。
所以我为播放器创建了一个图像,我的问题是,我想将图像移动到我的Hitbox方块上,以便碰撞可以很好地进行Video ---我的图像在碰撞方块之外,我有办法吗可以在正方形内移动图像?
我的玩家班级:
class player:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.speed = 5
self.isJump = False
self.JumpCount = 10
self.fall = 0
self.stand = pygame.image.load("stands.png")
self.lefts = [
pygame.image.load("Sprite-0001.png"),
pygame.image.load("Sprite-0002.png"),
pygame.image.load("Sprite-0003.png"),
pygame.image.load("Sprite-0004.png"),
pygame.image.load("Sprite-0005.png"),
pygame.image.load("Sprite-0006.png"),
pygame.image.load("Sprite-0007.png"),
pygame.image.load("Sprite-0008.png"),
pygame.image.load("Sprite-0009.png")
]
self.rights = [
pygame.image.load("Sprite-50.png"),
pygame.image.load("Sprite-51.png"),
pygame.image.load("Sprite-52.png"),
pygame.image.load("Sprite-53.png"),
pygame.image.load("Sprite-54.png"),
pygame.image.load("Sprite-55.png"),
pygame.image.load("Sprite-56.png"),
pygame.image.load("Sprite-57.png"),
pygame.image.load("Sprite-58.png")
]
self.rights = [pygame.transform.scale(image,(image.get_width()*4,image.get_height()*4)) for image in self.rights]
self.lefts = [pygame.transform.scale(image,(image.get_width()*4,image.get_height()*4)) for image in self.lefts]
self.stand = pygame.transform.scale(self.stand,(self.stand.get_width()*4,self.stand.get_height()*4))
self.bo_index = 0
self.anim_index = 0
self.stans_index = 0
self.direction = "right"
self.direction = "left"
self.direction = "standing"
self.rect = pygame.rect = pygame.Rect(self.x,self.y,width, height)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window, (255,255,0), self.rect)
if self.direction == "left":
window.blit(self.lefts[self.anim_index],self.rect)
self.anim_index += 1
if self.anim_index == len(self.lefts):
self.anim_index = 0
elif self.direction == "right":
window.blit(self.rights[self.anim_index],self.rect)
self.anim_index += 1
if self.anim_index == len(self.rights):
self.anim_index = 0
else:
window.blit(self.stand,self.rect)
我的完整代码
import pygame
pygame.init()
window = pygame.display.set_mode((500,500))
pygame.display.set_caption("Game")
plat = pygame.image.load("gt.png")
coinss = pygame.image.load("coin_gold.png")
slash = pygame.image.load("slash_03.png")
stand = pygame.image.load("bunny2_stand.png")
lefts = [pygame.image.load("Sprite-0001.png"),
pygame.image.load("Sprite-0002.png"),
pygame.image.load("Sprite-0003.png"),
pygame.image.load("Sprite-0004.png"),
pygame.image.load("Sprite-0005.png"),
pygame.image.load("Sprite-0006.png"),
pygame.image.load("Sprite-0007.png"),
pygame.image.load("Sprite-0008.png"),
pygame.image.load("Sprite-0009.png")
]
stand = pygame.image.load("stands.png")
rights = [pygame.image.load("Sprite-50.png"),
pygame.image.load("Sprite-51.png"),
pygame.image.load("Sprite-52.png"),
pygame.image.load("Sprite-53.png"),
pygame.image.load("Sprite-54.png"),
pygame.image.load("Sprite-55.png"),
pygame.image.load("Sprite-56.png"),
pygame.image.load("Sprite-57.png"),
pygame.image.load("Sprite-58.png")
]
class projectile(object):
def __init__(self,x,y,radius,color):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.speed = 10
self.slash = pygame.image.load("slash_03.png")
def draw(self,win):
pygame.draw.circle(win, self.color, (self.x,self.y), self.radius)
# the enemy
class enes:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
# color for enems
green = (63, 190, 22)
enems1 = enes(350,259,50,50, green)
florida = [enems1]
# player class
class player:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.speed = 5
self.isJump = False
self.JumpCount = 10
self.fall = 0
self.stand = pygame.image.load("stands.png")
self.lefts = [
pygame.image.load("Sprite-0001.png"),
pygame.image.load("Sprite-0002.png"),
pygame.image.load("Sprite-0003.png"),
pygame.image.load("Sprite-0004.png"),
pygame.image.load("Sprite-0005.png"),
pygame.image.load("Sprite-0006.png"),
pygame.image.load("Sprite-0007.png"),
pygame.image.load("Sprite-0008.png"),
pygame.image.load("Sprite-0009.png")
]
self.rights = [
pygame.image.load("Sprite-50.png"),
pygame.image.load("Sprite-51.png"),
pygame.image.load("Sprite-52.png"),
pygame.image.load("Sprite-53.png"),
pygame.image.load("Sprite-54.png"),
pygame.image.load("Sprite-55.png"),
pygame.image.load("Sprite-56.png"),
pygame.image.load("Sprite-57.png"),
pygame.image.load("Sprite-58.png")
]
self.rights = [pygame.transform.scale(image,(image.get_width()*4,image.get_height()*4)) for image in self.rights]
self.lefts = [pygame.transform.scale(image,(image.get_width()*4,image.get_height()*4)) for image in self.lefts]
self.stand = pygame.transform.scale(self.stand,(self.stand.get_width()*4,self.stand.get_height()*4))
self.bo_index = 0
self.anim_index = 0
self.stans_index = 0
self.direction = "right"
self.direction = "left"
self.direction = "standing"
self.rect = pygame.rect = pygame.Rect(self.x,self.y,width, height)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window, (255,255,0), self.rect)
if self.direction == "left":
window.blit(self.lefts[self.anim_index],self.rect)
self.anim_index += 1
if self.anim_index == len(self.lefts):
self.anim_index = 0
elif self.direction == "right":
window.blit(self.rights[self.anim_index],self.rect)
self.anim_index += 1
if self.anim_index == len(self.rights):
self.anim_index = 0
else:
window.blit(self.stand,self.rect)
# platforms
class platform:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.plat = pygame.image.load("gt.png")
self.rect = pygame.Rect(x,y,plat.get_width(), plat.get_height())
self.plat = pygame.transform.scale(self.plat,(self.plat.get_width()//2,self.plat.get_height()//2))
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
window.blit(self.plat,self.rect)
# Coins
class coin:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.coinss = pygame.image.load("coin_gold.png")
self.rect = pygame.Rect(x,y,coinss.get_width(), coinss.get_height())
self.plat = pygame.transform.scale(self.coinss,(self.coinss.get_width()//2,self.coinss.get_height()//2))
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
window.blit(self.coinss,self.rect)
# Floor
class floor:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
font = pygame.font.Font('freesansbold.ttf', 30)
score = 0
text = font.render('Gold = ' + str(score), True, (255,255,255))
textRect = text.get_rect()
textRect.center = (100, 40)
# enemy
Sfont = pygame.font.Font('freesansbold.ttf', 30)
Kills = 0
Stext = Sfont.render('Kills = ' + str(score), True, (255,255,255))
textRectS = Stext.get_rect()
textRectS.center = (400, 100)
# fps
FPS = 60
clock = pygame.time.Clock()
# colors
Green = (63, 190, 22)
Blue = (22, 190, 175)
white = (240, 240, 240)
# define the enemy player coin classes
playerman = player(140,350,30,30, Blue)
enemy1 = platform(150,390,190,10, Green)
enemy2 = platform(300,310,190,10, Green)
enemy3 = platform(80,260,190,10, Green)
enemy4 = platform(250,180,190,10, Green)
enemy5 = platform(490,120,190,10, Green)
enemy6 = platform(-50,100,190,10, Green)
enemy7 = platform(180,50,190,10, Green)
platforms = [enemy1,enemy2,enemy3,enemy4,enemy5,enemy6,enemy7]
# coin class
coin1 = coin(180,320,150,150, Green)
coin2 = coin(350,250,50,50, Green)
coin3 = coin(150,200,50,50, Green)
Coins_list = [coin1,coin2,coin3]
# floor class
floor1 = floor(-1000,490,9999,50, white)
flories = [floor1]
#main loop
bullets = []
runninggame = True
while runninggame:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
runninggame = False
if playerman.y < 250:
playerman.y += 1
for platform in platforms:
platform.y += playerman.speed
for coin in Coins_list:
coin.y += playerman.speed
for floor in flories:
floor.y += playerman.speed
for enes in florida:
enes.y += playerman.speed
if playerman.y > 450:
playerman.y -= playerman.fall
for platform in platforms:
platform.y -= playerman.fall
for coin in Coins_list:
coin.y -= playerman.fall
for floor in flories:
floor.y -= playerman.fall
for enes in florida:
enes.y -= playerman.fall
keys = pygame.key.get_pressed()
playerman.direction = "standing"
if keys[pygame.K_a]:
for bullet in bullets:
if bullet.x < 500 and bullet.x > 0:
bullet.x += bullet.speed
else:
bullets.pop(bullets.index(bullet))
if len(bullets) < 2:
bullets.append(projectile(round(playerman.x+playerman.width//2), round(playerman.y + playerman.height//2), 6, (0,0,0)))
if keys[pygame.K_d]:
for bullet in bullets:
if bullet.x < 500 and bullet.x > 0:
bullet.x -= bullet.speed
else:
bullets.pop(bullets.index(bullet))
if len(bullets) < 2:
bullets.append(projectile(round(playerman.x+playerman.width//2), round(playerman.y + playerman.height//2), 6, (0,0,0)))
if keys[pygame.K_LEFT]:
playerman.direction = "right"
playerman.x -= playerman.speed
if playerman.x < 100:
playerman.x += playerman.speed
for platform in platforms:
platform.x += playerman.speed
for coin in Coins_list:
coin.x += playerman.speed
for enes in florida:
enes.x += playerman.speed
if keys[pygame.K_RIGHT]:
playerman.direction = "left"
playerman.x += playerman.speed
if playerman.x > 400:
playerman.x -= playerman.speed
for platform in platforms:
platform.x -= playerman.speed
for coin in Coins_list:
coin.x -= playerman.speed
for enes in florida:
enes.x -= playerman.speed
if not playerman.isJump:
playerman.y += playerman.fall
playerman.fall += 1
playerman.isJump = False
collide = False
for platform in platforms:
if playerman.rect.colliderect(platform.rect):
collide = True
playerman.isJump = False
playerman.y = platform.rect.top - playerman.height + 1
if playerman.rect.right > platform.rect.left and playerman.rect.left < platform.rect.left - playerman.width:
playerman.x = platform.rect.left - playerman.width
if playerman.rect.left < platform.rect.right and playerman.rect.right > platform.rect.right + playerman.width:
playerman.x = platform.rect.right
for i in range(len(Coins_list)-1,-1,-1):
if playerman.rect.colliderect(Coins_list[i].rect):
del Coins_list[i]
score += 1
text = font.render('Score = ' + str(score), True, (255,255,255))
textRect = text.get_rect()
textRect.center = (100, 40)
for floor in flories:
if playerman.rect.colliderect(floor.rect):
collide = True
playerman.isJump = False
playerman.y = floor.rect.top - playerman.height + 1
if playerman.rect.right > floor.rect.left and playerman.rect.left < floor.rect.left - playerman.width:
playerman.x = floor.rect.left - playerman.width
if playerman.rect.left < floor.rect.right and playerman.rect.right > floor.rect.right + playerman.width:
playerman.x = floor.rect.right
if playerman.rect.bottom >= 490:
collide = True
playerman.isJump = False
playerman.JumpCount = 10
playerman.y = 490 - playerman.height
if collide:
if keys[pygame.K_UP]:
playerman.isJump = True
playerman.fall = 0
else:
if playerman.JumpCount > 0:
playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
playerman.JumpCount -= 1
else:
playerman.JumpCount = 10
playerman.isJump = False
window.fill((74, 107, 104))
enems1.draw()
window.blit(Stext,textRectS)
for bullet in bullets:
bullet.draw(window)
window.blit(text,textRect)
for platform in platforms:
platform.draw()
for coin in Coins_list:
coin.draw()
playerman.draw()
for floor in flories:
floor.draw()
pygame.display.update()
pygame.quit()
答案
问题是,图像(pygame.Surface
)比击中框更光滑。因此,您必须创建一个pygame.Rect
对象,该对象的图像大小应与点击框的中心点相同:
pygame.Rect
首先确定当前帧的图像,然后计算player_rect = player_image.get_rect(center = self.rect.center)
window.blit(player_image, player_rect)
,然后player_rect
计算图像:
blit
以上是关于我如何在HitBox中移动图像? pygame的主要内容,如果未能解决你的问题,请参考以下文章