Необходимо, чтобы игрок прыгал по платформам. Желательно чтоб коллизия срабатывала только сверху вниз на платформу, но не обязательно. Сейчас коллизия срабатывает, но положение игрока скачет при каждом прыжке.
Вот ссылка на сам проект: https://drive.google.com/file/d/1V-AlNgjXTsAZS2hkkgnMk_6tHRn5mFaA/view?usp=sharing
import pygame
import random
class Sprite():
def __init__ (self, x, y, speed, img, speedy, jumppower):
self.image = pygame.image.load(img)
self.speed = speed
self.speedx = 0
self.speedy = speedy
self.jumppower = jumppower
self.rect = self.image.get_rect(topleft = (x, y))
self.width = self.image.get_rect().width
self.height = self.image.get_rect().height
class Ground(pygame.sprite.Sprite):
def __init__ (self, x, y, img):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(img)
self.rect = self.image.get_rect(topleft = (x, y))
WIDTH = 1200
HEIGHT = 760
FPS = 30
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()
pygame.display.flip()
bg = pygame.image.load("image/background1.png")
pygame.display.set_icon(pygame.image.load("image/icon.png"))
isJump = False
g = 1
numbers = ["UP"]
bg_x = 0
nCadr=0
PLAYERWalk = ["image/playerAnim/Walk/1.png",
"image/playerAnim/Walk/2.png",
"image/playerAnim/Walk/3.png",
"image/playerAnim/Walk/4.png",
"image/playerAnim/Walk/5.png",
"image/playerAnim/Walk/6.png"]
PLAYER = Sprite(WIDTH/4, -30, 10, "image/playerAnim/Walk/1.png", 10, 20)
grounds = pygame.sprite.Group()
grounds.add(Ground(100, 300, 'image/grass/grassHalf.png'))
grounds.add(Ground(250, 450, "image/grass/grassHalf.png"))
grounds.add(Ground(450, 200, "image/grass/grassHalf.png"))
grounds.add(Ground(1500, 200, "image/grass/grassHalf.png"))
grounds_list = list(grounds)
keys = pygame.key.get_pressed()
running = True
while running:
nCadr += 1
# Держим цикл на правильной скорости
clock.tick(FPS)
# Ввод процесса (события)
for event in pygame.event.get():
# проверка нажатия на кнопку закрыть
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
PLAYER.image = pygame.image.load(PLAYERWalk[nCadr % len(PLAYERWalk)])
bg_x -= PLAYER.speed / 2
for ground in grounds:
ground.rect.x -= PLAYER.speed
elif keys[pygame.K_LEFT]:
PLAYER.image = pygame.transform.flip(pygame.image.load(PLAYERWalk[nCadr % len(PLAYERWalk)]), True, False)
bg_x += PLAYER.speed / 2
for ground in grounds:
ground.rect.x += PLAYER.speed
if not isJump:
if keys[pygame.K_UP]:
isJump = True
else:
if PLAYER.jumppower >= 0:
PLAYER.rect.y -= (PLAYER.jumppower ** 2) / 20
PLAYER.jumppower -= 1
else:
isJump = False
PLAYER.jumppower = 20
index_of_collision = pygame.sprite.spritecollideany(PLAYER, grounds)
if index_of_collision is None:
PLAYER.speedy += g
#print("no")
elif PLAYER.speedy > 0:
PLAYER.speedy = 0
#print("yes")
PLAYER.rect.y += PLAYER.speedy
if bg_x == -WIDTH or bg_x == WIDTH:
bg_x = 0
screen.blit(bg, (bg_x - WIDTH, 0))
screen.blit(bg, (bg_x, 0))
screen.blit(bg, (bg_x + WIDTH, 0))
grounds.draw(screen)
screen.blit(PLAYER.image, (PLAYER.rect.topleft))
pygame.display.update()
pygame.quit()
update()
. Когда вы это делаете, движок PyGame не знает, что объект был перемещен, и продолжает рисовать его в исходном положении.