如何使我的玩家与平台pygame碰撞

时间:2020-06-30 17:58:24

标签: python pygame

如何使播放器与平台碰撞?

我知道如何使球员移动,跳跃以及如何增加围墙。我也知道如何为我的播放器制作图像,如何制作形状以及python和其他东西的大多数基础知识。我正在学习python,并且是pygame的新手。

这是我的播放器代码:

class player:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.speed = 5
        self.isJump = False
        self.JumpCount = 10
        self.ss1 = pygame.image.load("G5.png")
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        window.blit(self.ss1,self.rect)


这是我的完整代码:

import pygame
pygame.init()

#this is screem height
window = pygame.display.set_mode((500,500))

#know we put screem name
pygame.display.set_caption("Noobs first Game")

#player class
class player:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.speed = 5
        self.isJump = False
        self.JumpCount = 10
        self.ss1 = pygame.image.load("G5.png")
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        window.blit(self.ss1,self.rect)

class enemy:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft=(self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)


#player and enemy
white = (255,255,255)
player1 = player(0,400,60,60,white)

red = (255,48,48)
enemy1 = enemy(100,100,60,20,red)

#window
def redrawwindow():
    window.fill((0,0,0))

#player draw
    player1.draw()
    enemy1.draw()

fps = (30)
clock = pygame.time.Clock()

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


    keys = pygame.key.get_pressed()

    if keys[pygame.K_a]and player1.x > player1.speed:
        player1.x -= player1.speed

    if keys[pygame.K_d]and player1.x <500 - player1.height - player1.speed:
        player1.x += player1.speed

    if keys[pygame.K_w]and player1.y > player1.speed:
        player1.y -= player1.speed

    if keys[pygame.K_s]and player1.y <500 - player1.width - player1.speed:
        player1.y += player1.speed

    if not(player1.isJump):
        if keys[pygame.K_SPACE]:
            player1.isJump = True

    else:
        if player1.JumpCount >= -10:
            player1.y -= (player1.JumpCount*abs(player1.JumpCount))*0.5
            player1.JumpCount -= 1
        else:
            player1.isJump = False
            player1.JumpCount = 10




            
    redrawwindow()
    pygame.display.update()
pygame.quit()



```

1 个答案:

答案 0 :(得分:0)

首先,欢迎堆栈溢出。您的第一个问题是好的,因为您提供了详细信息,代码,并且特定于问题。只需尝试修正语法和标点符号,使其更易于阅读。

就您的问题而言,您想检查播放器是否发生碰撞

if player.rect.colliderect(enemy.rect):

然后相应地移动它们。例如,检查速度并将其沿相反方向少量移动。