编辑:很快:您应该使用<= 而不是== 和[1] 而不是[0]
if y1 <= platforms[i][1]:
print ("HIT")
y1 = platforms[i][1] # move up with platform
首先你应该使用pygame.Rect() 来保持位置和大小。它有属性xywidth,height,但也有用bottom,top,center等。
它还有检查两个Rect()之间碰撞的方法
player_rect.colliderect(platform_rect)
当玩家和平台发生碰撞时,你必须使用平台的top 更改玩家的bottom
if player_rect.colliderect(platform_rect):
player_rect.bottom = platform_rect.top
它会随着平台移动玩家。
最少的工作代码:红色平台向上移动,玩家也自动向上移动。
import pygame
# --- constants --- (UPPER_CASE_NAMES)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 25 # for more than 220 it has no time to update screen
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# --- functions --- (lower_case_names)
# --- main ---
pygame.init()
screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )
screen_rect = screen.get_rect()
player_rect = pygame.Rect(0,0,50,50)
player_color = (0,255,0)
player_rect.centerx = screen_rect.centerx
player_rect.bottom = screen_rect.bottom-25
platform_rect = pygame.Rect(0,0,200,25)
platform_color = (255,0,0)
platform_rect.centerx = screen_rect.centerx
platform_rect.bottom = screen_rect.bottom
# --- mainloop ---
clock = pygame.time.Clock()
running = True
while running:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
running = False
# --- changes/moves/updates ---
platform_rect.y -= 1
if player_rect.colliderect(platform_rect):
player_rect.bottom = platform_rect.top
# --- draws ---
screen.fill(BLACK)
pygame.draw.rect(screen, player_color, player_rect)
pygame.draw.rect(screen, platform_color, platform_rect)
pygame.display.flip()
# --- FPS ---
ms = clock.tick(FPS)
# --- end ---
pygame.quit()
编辑: 平台向下移动的示例。当平台向下移动时,它使用player_gravity 将玩家保持在平台上。
如果player_gravity 与plaftorm_speed 相同,则玩家停留在平台上 - 但player_gravity 小于plaftorm_speed,则玩家下落速度比平台慢(如降落伞)
import pygame
# --- constants --- (UPPER_CASE_NAMES)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 25 # for more than 220 it has no time to update screen
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# --- functions --- (lower_case_names)
# --- main ---
pygame.init()
screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )
screen_rect = screen.get_rect()
player_rect = pygame.Rect(0,0,50,50)
player_color = (0,255,0)
player_rect.centerx = screen_rect.centerx
player_rect.bottom = screen_rect.bottom-25
player_gravity = 3 # try 5
platform_rect = pygame.Rect(0,0,200,25)
platform_color = (255,0,0)
platform_rect.centerx = screen_rect.centerx
platform_rect.bottom = screen_rect.bottom
platform_direction = 'top'
platform_speed = 5
# --- mainloop ---
clock = pygame.time.Clock()
running = True
while running:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
running = False
# --- changes/moves/updates ---
if platform_direction == 'top':
platform_rect.y -= platform_speed
if platform_rect.top <= 100:
platform_direction = 'bottom'
else:
platform_rect.y += platform_speed
if platform_rect.bottom >= 600:
platform_direction = 'top'
player_rect.y += player_gravity
if player_rect.colliderect(platform_rect):
player_rect.bottom = platform_rect.top
# --- draws ---
screen.fill(BLACK)
pygame.draw.rect(screen, player_color, player_rect)
pygame.draw.rect(screen, platform_color, platform_rect)
pygame.display.flip()
# --- FPS ---
ms = clock.tick(FPS)
# --- end ---
pygame.quit()