Я пробовал много разных способов, но не могу заставить мою систему обнаружения столкновений работать - PullRequest
0 голосов
/ 05 ноября 2018

Вот мой код. Я бы предпочел не использовать спрайт для обнаружения столкновения. Я пытаюсь сделать версию каракули, где мой персонаж подпрыгивает на платформах. Спасибо!

# #

import pygame
import random
from random import randint
pygame.init()
WINDOW_HEIGHT = 700
WINDOW_WIDTH = 400
PLATFORM_WIDTH = 75
PLATFORM_HEIGHT = 25
player_height = 75
player_width  = 50
x5 = 200
y5 = 620
class Platform(object):
    def __init__(self):
        self.x = 0
        self.y = 0
        self.change_x = 0
def make_platform_green():
    platform = Platform()
    platform.x = random.randrange(0, 625)
    platform.y = random.randrange(PLATFORM_HEIGHT, WINDOW_HEIGHT - PLATFORM_HEIGHT)

    return platform

def make_platform_blue():
    platform1 = Platform()
    platform1.x = random.randrange(0, 625)
    platform1.y = random.randrange(PLATFORM_HEIGHT, WINDOW_HEIGHT - PLATFORM_HEIGHT)
    platform1.change_x = random.randrange(1, 2)

    return platform1

Основной цикл

def main():

    size = [WINDOW_HEIGHT, WINDOW_HEIGHT]
    window = pygame.display.set_mode(size)
    Score = 0
    x5 = 200
    y5 = 620
    isJump = False
    jumpCount = 10
    player_height = 75
    player_width  = 50
    make_blue = False
    make_green = False
    vel = 5
    done = False
    clock = pygame.time.Clock()
    platform_list = []
    platform1_list = []
    platform = make_platform_green()
    platform1 = make_platform_blue()
    platform_list.append(platform)
    platform1_list.append(platform1)
    clock.tick(60)

Вот код столкновения, который я не могу понять. Предполагается проверить, есть ли пиксель моего плеера в пикселе платформы.

    def collision():
        for i in range(x5, x5 + player_width):
            for b in range(y5, y5 + player_height):
                if i >= platform.x and i >= platform.x + PLATFORM_WIDTH and b <= platform.y and b <= platform.y + PLATFORM_HEIGHT:
                    isJump = True

#

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
        for platform1 in platform1_list:
            platform1.x += platform1.change_x
            if platform1.x  == 0 or platform1.x == 625:
                platform1.change_x *= -1
        keys = pygame.key.get_pressed()
        if keys[pygame.K_RIGHT]:
        x5 += vel
        if keys[pygame.K_LEFT]:
            x5 -= vel
        #Switches charecter to other side
        if x5 <= -50:
            x5 = 650
        if x5 >= 700:
            x5 = 0

Мой код перехода, который должен выполняться при столкновении

        #Bounce code
        if not (isJump):
            if y5 == 620:
                isJump = True
        else:
            if jumpCount >= -10:
                neg = 1
                if jumpCount < 0:
                    neg = -1
                y5 -= (jumpCount ** 2) * 0.2 * neg
                jumpCount -= 0.5
            else:
                isJump = False
                jumpCount = 10
        #Platform Spawner Spawns 1
        if Score >= 101:
            make_green = True
        if Score >= 101.1:
            make_green = False
        if Score >= 201:
            make_blue = True
        if Score >= 201.1:
            make_blue = False
        if make_green == True:
            platform = make_platform_green()
            platform_list.append(platform)
        if make_blue == True:
            platform1 = make_platform_blue()
            platform1_list.append(platform1)
        #Window fill must be before drawing
        window.fill((154, 205, 50))
        #Draws Platforms
        for platform in platform_list:
            pygame.draw.rect(window, (48, 128, 20), (platform.x, platform.y, PLATFORM_WIDTH, PLATFORM_HEIGHT ))
        for platform1 in platform1_list:
            pygame.draw.rect(window, (0, 0, 200), (platform1.x, platform1.y, PLATFORM_WIDTH, PLATFORM_HEIGHT))
        pygame.draw.rect(window, (238, 18, 137), (0, 695, 800, 5))
        pygame.draw.rect(window, (0, 0, 255), (x5, y5, player_width, player_height))
        if y5 == 620 or isJump == True:
            Score += 1
        collision()
        pygame.display.set_caption('Score: {}'.format(Score))
        pygame.display.update()
        pygame.display.flip()
    pygame.quit()
if __name__ == '__main__':
    main()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...