Есть ли лучший способ не заставлять прямоугольники сталкиваться друг с другом в pygame? [дубликат]Python

Программы на Python
Anonymous
 Есть ли лучший способ не заставлять прямоугольники сталкиваться друг с другом в pygame? [дубликат]

Сообщение Anonymous »

Итак, я делаю эту игру, и у меня возникла проблема: мой персонаж всегда заскакивал в здание, когда вы прыгали в него сбоку. Я исправил это сейчас, проверив, находится ли мой персонаж в здании, но это сработает только в этой ситуации, так что есть способ получше, верно? Также извините, что не оставил комментариев.
Полный код:
import pygame
from sys import exit

buildings = []
list = []

screen = pygame.display.set_mode((720, 720))
clock = pygame.time.Clock()

player_image = pygame.image.load("MyBoy.png").convert_alpha()
player_image = pygame.transform.scale(player_image, (100, 180))
player_rect = player_image.get_rect(midbottom=(200, 600))

building_length = 500
building = pygame.Rect((0, 600, building_length, 120))
touching_builiding = False

run = True

vel = 10
jump = False
jumpCount = 0
jumpMax = 15

while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

keys = pygame.key.get_pressed()

if keys[pygame.K_SPACE]:
if not jump:
jump = True
jumpCount = jumpMax
if keys[pygame.K_F4]:
player_rect.x = 200
player_rect.y = 420
jump = False
jumpCount = 0

player_rect.centerx = (player_rect.centerx + (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel)

if jump:
player_rect.y -= jumpCount
if not player_rect.colliderect(building):
jumpCount -= 1
else:
jump = False

if not jump and not touching_builiding and not player_rect.colliderect(building):
player_rect.y -= jumpCount
jumpCount -= 1

if player_rect.x < 500 and player_rect.y > 421 and player_rect.colliderect(building):
player_rect.x = 500
touching_builiding = True
player_rect.y -= jumpCount
jumpCount -= 1
else:
touching_builiding = False

screen.fill("white")
screen.blit(player_image, player_rect)
pygame.draw.rect(screen, (255, 0, 0), building)
pygame.display.flip()
clock.tick(60)
print(player_rect.x, player_rect.y)

pygame.quit()
exit()



Подробнее здесь: https://stackoverflow.com/questions/797 ... -in-pygame

Вернуться в «Python»