如何解决你如何在 pygame 平台游戏中进行碰撞物理?
我正在 pygame 中为学校项目开发平台游戏。 这是指向我的 repl.it 的链接以供参考。
就碰撞而言,我们只是在每次 mario 接触块时在控制台中打印“hit”。继续前进,我不知道我应该如何进行碰撞或物理将如何工作。任何形式的帮助将不胜感激。
import pygame
pygame.init()
#set the background
screen = pygame.display.set_mode((1600,800))
clock = pygame.time.Clock()
FPS = 45
vel = 3.5
BLACK = (0,0)
back = pygame.image.load('background.png')
background = pygame.transform.scale(back,(800,600))
playerimg = pygame.image.load('mariosprite.png').convert_alpha()
#playerimgflipped = pygame.image.load('normalmarioflipped.png')
#playerimgjump = pygame.image.load('finishedjumpingmario.png')
blockonelol = pygame.image.load('oneblock.png')
blockoneX = 100
blockoneY = 415
blockone = pygame.transform.scale(blockonelol,(36,36))
mario = pygame.transform.scale(playerimg,(35,50))
playerX = 10
playerY = 490
isJump = False
jumpCount = 11
run = True
while run:
clock.tick(FPS)
screen.fill(BLACK)
screen.blit(background,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
mario_rect = mario.get_rect(topleft = (playerX,playerY))
blockone_rect = blockone.get_rect(topleft = (blockoneX,blockoneY))
condition = mario_rect.colliderect(blockone_rect)
if keys[pygame.K_LEFT] and vel < playerX:
playerX = playerX - vel
#mario = pygame.transform.scale(playerimgflipped,50))
if mario_rect.bottom+10 <=blockone_rect.top:
print('standing')
if keys[pygame.K_RIGHT] and condition == False:
playerX+=vel
mario = pygame.transform.scale(playerimg,50))
if not(isJump) and condition == False:
if keys[pygame.K_UP]:
isJump = True
if(playerY == 490):
mario = pygame.transform.scale(playerimg,50))
else:
#mario = pygame.transform.scale(playerimgjump,(45,55))
if jumpCount >= -11:
goingdown = 1
if jumpCount < 0:
goingdown = -1
playerY -= (jumpCount ** 2) * 0.25 * goingdown
jumpCount = jumpCount - 1
if(playerY == 490):
mario = pygame.transform.scale(playerimg,50))
elif condition == True:
jumpCount = 0
else:
isJump = False
jumpCount = 11
if(playerY == 490):
mario = pygame.transform.scale(playerimg,50))
if mario_rect.colliderect(blockone_rect):
print("hit")
screen.blit(blockone,(blockoneX,blockoneY))
screen.blit(mario,(playerX,playerY))
pygame.display.update()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。