如何创建碰撞检测?

时间:2019-12-13 16:44:26

标签: python pygame

我有一个精灵,周围画有一个矩形,还有一个生成的矩形块,您必须在上面跳一下。我不知道如何使用sprite.colliderect或它是什么。我想将其放置在您从地面跳到矩形块上,在其上着陆然后再跳到另一个矩形块上的位置。我显然会自己做后者,但我不太了解Pygame的碰撞方式。

有人可以教我吗?

import pygame
from random import randrange
pygame.init()

#Setting Variables
screenW = 1020
screenH = 630

velocity = 7
platX = 0
platY = 0
#Variable Setting #2
left = False
right = False
#FPS
clock = pygame.time.Clock()
clock.tick(60)

#Screen
walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'), pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'), pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'), pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'), pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]
char = pygame.image.load('standing.png')
window = pygame.display.set_mode((screenW,screenH))
pygame.display.set_caption(("template base"))
bg = pygame.image.load('bg.png')

class platform():
    def __init__ (self):
        self.platX = randrange(0, 850)
        self.platY = randrange(320, 520)
        print(self.platX, self.platY)
    def draw(self):
        pygame.draw.rect(window, (255, 255, 255), (self.platX, self.platY, 350, 50))

class player():
    def __init__(self):
        self.x = 700
        self.y = 500
        self.width = 50
        self.height = 50
        self.walkCount = 0
        self.isJump = False
        self.jumpCount = 10
        self.left = False
        self.right = False
    def move(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and self.x > velocity:
            self.x -= velocity
            self.left = True
            self.right = False
        elif keys[pygame.K_RIGHT] and self.x < screenW - velocity - self.width:
            self.x += velocity
            self.right = True
            self.left = False
        else:
            self.right = False
            self.left = False
            self.walkCount = 0
        if not self.isJump:
            if keys[pygame.K_SPACE]:
                self.isJump = True
        else:
            if self.jumpCount >= -10:
                self.y -= (self.jumpCount * abs(self.jumpCount)) * 0.5
                self.jumpCount -= 1
            else:
                self.jumpCount = 10
                self.isJump = False

    def draw(self):
        if self.walkCount + 1 >= 27:
            self.walkCount = 0
        elif self.left == True:
            window.blit(walkLeft[self.walkCount // 3], (self.x, self.y))
            self.walkCount += 1
        elif self.right == True:
            window.blit(walkRight[self.walkCount // 3], (self.x, self.y))
            self.walkCount += 1
        else:
            window.blit(char, (self.x, self.y))
            self.walkCount = 0
            pygame.draw.rect(window, (0, 0, 0), ((self.x + 10), (self.y + 5), 42, 62), 2)
def redrawGameWindow():
    window.blit(bg, (0, 0))
    player.draw()
    player.move()
    platform.draw()
    pygame.display.update()

platform = platform()
player = player()
#Main Loop
run = True
while run:
    pygame.time.delay(15)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    #Drawing

    redrawGameWindow()


pygame.quit()

0 个答案:

没有答案