如何在pygame中使玩家与平台之间发生冲突

时间:2020-04-02 14:42:17

标签: pygame

我正在尝试制作平台游戏,但是我在做一些事情方面的努力。 首先,玩家可以跳过应该发生的物体。 其次,我正在努力寻找一种方法来注册程序,证明玩家在“地面”上,但由于冲突而注册。

任何帮助将不胜感激。

代码如下:

import pygame
pygame.init()
screenwidth = 696
screenheight = 652

screen =pygame.display.set_mode((screenwidth,screenheight))
bg = pygame.image.load('Wizard Background 3.png')
platform_image = pygame.image.load('Image1.png')

class Platform (object):


    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = 50
        self.heigh = 50
        self.hitbox = (self.x -3 , self.y -3, 55, 55)

    def draw(self,screen):

        screen.blit(platform_image, (self.x,self.y))
        pygame.draw.rect(screen, (255,0,0), self.hitbox, 2)



class Player (object):

    def __init__ (self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height

        self.vel = 5
        self.isJump = False
        self.jumpCount = 10
        self.hitbox = (self.x -3 , self.y -3, 35, 35)

    def draw (self,screen):
        pygame.draw.rect(screen, (255,0,0), (self.x, self.y, self.width, self.height))

        self.hitbox = (self.x -3 , self.y -3 , 35, 35)
        pygame.draw.rect(screen, (255,0,0), self.hitbox, 2)

    def collision (self):
       print("OOF")
       self.x += 10
       self.y += 10


def check_collision():
    global collision
    for i in range(num_of_platforms):
        if player.hitbox[1]  < platforms[i].hitbox[1] + platforms[i].hitbox[3] and player.hitbox[3] + player.hitbox [1] > platforms[i].hitbox[1]:
            if player.hitbox[0]  < platforms[i].hitbox[0] + platforms[i].hitbox[2] and player.hitbox[0] + player.hitbox [2]> platforms[i].hitbox[0]:
                player.collision()
                #player.isJump = False
                collision = True
        else:
            collision = False


def redrawGameWindow():

    screen.blit(bg, (0,0))
    player.draw(screen)
    for i in platforms:
        i.draw(screen)

    pygame.display.update()

platforms = []
platforms.append(Platform(0*50, 7*50))
platforms.append(Platform(0*50, 10*50))
platforms.append(Platform(1*50, 10*50))
platforms.append(Platform(2*50, 10*50))
platforms.append(Platform(3*50, 10*50))
platforms.append(Platform(4*50, 10*50))
platforms.append(Platform(5*50, 10*50))
platforms.append(Platform(6*50, 10*50))
platforms.append(Platform(7*50, 10*50))
platforms.append(Platform(8*50, 10*50))
platforms.append(Platform(9*50, 10*50))
platforms.append(Platform(10*50, 10*50))

num_of_platforms = len(platforms)
print(num_of_platforms)

player = Player(50, 410, 30, 30)
run = True
while run:


    check_collision()


#If the user presses the close button, the while loop will stop leading it to end the program
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    if collision == False:
        if keys[pygame.K_LEFT] and player.x > player.vel:
            player.x -= player.vel

        elif keys[pygame.K_RIGHT] and player.x < (screenwidth - player.width - player.vel):
            player.x+= player.vel

        elif keys[pygame.K_DOWN] and player.y < (screenheight - player.height - player.vel):
            player.y+= player.vel

        if not(player.isJump):
            if keys[pygame.K_UP]:
                    player.isJump = True
                    player.walkCount = 0
        else:
            if player.jumpCount >= -10:
                neg = 1
                if player.jumpCount<0:
                    neg = -1     
                player.y -= player.jumpCount ** 2 * 0.2 * neg
                player.jumpCount -=1
                check_collision()

            else:
                player.isJump = False
                player.jumpCount = 10







    redrawGameWindow()

pygame.quit()

0 个答案:

没有答案