首页 > 解决方案 > 如何让我的玩家与平台 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()



```

标签: pythonpygame

解决方案


首先,欢迎堆栈溢出。您的第一个问题很好,因为您提供了详细信息、代码并且问题具体。试着修正你的语法和标点符号,让它更容易阅读。

就您的问题而言,您想检查播放器是否与

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

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


推荐阅读