Создание коллизии в Pygame
Нужно, чтобы человечек при соприкосновении с прямоугольником не мог опуститься вниз, но мог упасть скраю.
import pygame
import random
res_shir = 1080
res_vis = 900
pygame.init()
isJump = False
jumpCount = 10
window = pygame.display.set_mode((res_shir, res_vis))
pygame.display.set_caption("Моя первая игра")
player = pygame.image.load("imgonline-com-ua-Resize-NwejEOZb9SDL7E.jpg")
bg = pygame.image.load("imgonline-com-ua-Resize-dTQqF4gM9xPTOI.jpg")
x = 50
y = 850
speed = 5
class Player(pygame.sprite.Sprite):
def __init__(self):
super.__init__()
self.image = pygame.image.load("imgonline-com-ua-Resize-NwejEOZb9SDL7E.jpg")
self.rect = self.image.get_rect()
self.change_x = 0
self.change_y = 0
self.level = None
def update(self):
self.calc_gravity()
self.rect.x += self.change_x
class Level(object):
def __init__(self):
self.platforms = pygame.sprite.Group()
self.player = player
self.background = None
def update(self):
screen.blit(bg, (0, 0))
self.platforms.draw(super)
clock = pygame.time.Clock()
run = True
if run == True:
obj1_rand = random.randint(100, 980), 830
obj2_rand = random.randint(100, 980), 680
obj3_rand = random.randint(100, 980), 530
obj1 = pygame.Surface((150,50))
obj2 = pygame.Surface((150,50))
obj3 = pygame.Surface((150,50))
while (run):
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x >= 0:
x -= speed
if keys[pygame.K_RIGHT] and x <= res_shir - 100:
x += speed
if keys[pygame.K_q]:
pygame.QUIT()
if not(isJump):
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
if jumpCount < 0:
y += (jumpCount ** 2) / 2
else:
y -= (jumpCount ** 2) / 2
jumpCount -= 1
else:
isJump = False
jumpCount = 10
window.blit(bg, (0, 0))
window.blit(player, (x, y))
window.blit(obj1, (obj1_rand))
window.blit(obj2, (obj2_rand))
window.blit(obj3, (obj3_rand))
pygame.display.update()