Pygame碰撞不断的碰撞?

时间:2015-08-19 15:06:18

标签: python pygame sprite collision-detection

谢谢BlackJack,这里有你的建议代码。玩家图像只停留在左上角(0,0)。图像循环遍历转轮阵列中的图像,但向左或向右按​​下不会使玩家精灵在背景图像的边缘移动,也不会移动背景图像和块。

import pygame, math
from math import *


backimage = pygame.image.load("C:/Users/Jed/Desktop/Drawings/DSC_0001_cropped.jpg")
cameraX_offset = 0
cameraY_offset = 0
screen = pygame.display.set_mode([700,700])

pygame.init()
pygame.key.set_repeat(20, 20)

#Runner instances#
runner = []
#lots of images from disk add to array
currentRunner = 17
#end of runners!#



freespeed = 5   #speed character moves at edges of background
jumping = False
jumpingLeft = True
jumpVariable = 0
initialY = None

class Player(pygame.sprite.Sprite):
    def __init__(self):
        self.image = runner[int(floor(currentRunner))]
        self.rect = self.image.get_rect()
        self.rect.topleft = (350, 500)

    def see_on_block(self):
        new_rect = self.rect.move(0, 2)
        if not any(new_rect.colliderect(b.rect) for b in blocks):
                self.rect= new_rect

    def movement (self):
        global cameraX_offset, currentRunner, jumping, jumpingLeft, jumpVariable, initialY
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:                  #moving character left
                    if cameraX_offset <= 0 and self.rect.left > 20:     
                        self.rect.left -= freespeed
                    elif 350 < self.rect.left <= 800:
                        self.rect.left -= freespeed
                    elif cameraX_offset > 0:
                        cameraX_offset -=1
                    if currentRunner >= 7:
                        currentRunner = 0
                    else:
                        currentRunner += 0.3

                if event.key == pygame.K_RIGHT:                 #moving character right
                    if cameraX_offset >= 250 and self.rect.left < 635:
                        self.rect.left += freespeed
                    elif -10 <= self.rect.left < 350:
                        self.rect.left += freespeed
                    elif cameraX_offset < 250:
                        cameraX_offset += 1
                    if currentRunner < 8 or



currentRunner >= 15:
                        currentRunner = 8
                    else:
                        currentRunner += 0.3

                if event.key == pygame.K_SPACE:
                    jumping = True
                    initialY = self.rect.top

                elif event.type == pygame.KEYUP:
                    if 0 <= currentRunner <= 7:
                        currentRunner = 16
                    elif 8 <= currentRunner <= 15:
                        currentRunner = 17

        if jumping == True:
            if 0 <= currentRunner <= 7 or currentRunner == 16 or 18 <= currentRunner <= 21: #sees whether character is facing left or right when jump initiated
                jumpingLeft = True
            else:
                jumpingLeft = False
            jumpVariable += (radians(5))

            if radians(90) <= jumpVariable < radians(270):
                self.rect.top = initialY - 100*sin(jumpVariable - radians(90))  #moves character in Y according to sin
                if jumpingLeft == True:
                    if cameraX_offset <= 0 and self.rect.left > 20:         #moves character in X if facing left
                        self.rect.left -= jumpVariable
                    elif 350 < self.rect.left <= 800:
                        self.rect.left -= jumpVariable
                    elif cameraX_offset > 0:
                        cameraX_offset -= 2
                elif jumpingLeft == False:
                    if cameraX_offset >= 250 and self.rect.left < 635:      #moves character in X if facing right
                        self.rect.left += jumpVariable
                    elif -10 <= self.rect.left < 350:
                        self.rect.left += jumpVariable
                    elif cameraX_offset < 250:
                        cameraX_offset += 2

            if jumpVariable >= radians(270):                        #changes character's sprite when jumping according to variable
                jumping = False
                jumpVariable = 0
            if  0 < jumpVariable < radians(45):
                if jumpingLeft == True:
                    currentRunner = 18
                else:
                    currentRunner = 22
            elif radians(45) <= jumpVariable < radians(70):
                if jumpingLeft == True:
                    currentRunner = 19
                else:
                    currentRunner = 23
            elif radians(70) <= jumpVariable < radians (105):
                if jumpingLeft == True:
                    currentRunner = 20
                else:
                    currentRunner = 24
            elif radians(105) <= jumpVariable < radians (225):
                if jumpingLeft == True:
                    currentRunner = 21
                else:
                    currentRunner = 25
            elif radians(225) <= jumpVariable <= radians(260):
                if jumpingLeft == True:
                    currentRunner = 18
                else:
                    currentRunner = 22
            elif radians(260) < jumpVariable < radians(270):
                if jumpingLeft == True:
                    currentRunner = 16
                else:
                    currentRunner = 17

        if jumping == False:
            jumpVariable = 0

        self.image = runner[int(floor(currentRunner))]
        self.rect = self.image.get_rect()
        screen.blit(self.image, (self.rect.left, self.rect.top))


class Block(pygame.sprite.Sprite):
    def __init__(self, blockX, blockY, block_width, block_height):
        self.blockX = blockX
        self.blockY = blockY
        self.block_width = block_width
        self.block_height = block_height

    def draw_block(self):
        self.rect = pygame.draw.rect(screen,[160, 160, 160], (self.blockX - 2*cameraX_offset, self.blockY - cameraY_offset, self.block_width, self.block_height))


##create object instances##
blocks = []
floorBlock = Block (0, 680, 1200, 20)
blocks.append(floorBlock)
leftWall = Block (0 , 0, 20, 700)
blocks.append(leftWall)
rightWall = Block (1180 , 0, 20, 700)
blocks.append(rightWall)
block1 = Block (450, 600, 100, 20)
blocks.append(block1)
player = Player()


##main loop##
running = True    
while running:

    pygame.time.Clock().tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.blit(backimage, (0-cameraX_offset, 0-cameraY_offset))

    for b in blocks:
        b.draw_block()


    player.movement()
    player.see_on_block()
    pygame.display.flip()

pygame.quit()

2 个答案:

答案 0 :(得分:0)

将矩形设置为播放器的代码部分非常重要。您最初将跑步者的矩形设置为与图像的矩形相同,结果为(0,0,宽度,高度)。

因为跑步者四处走动,你必须考虑它的位置,并在你的see_on_block中修改矩形的位置(矩形的前两个值)。

    def see_on_block(self):
    self.Y += 2
    tempRect = (self.rect[0] + self.X, self.rect[1] + self.Y, self.rect[2], self.rect[3]);
    for b in blocks:
        if b.rect.colliderect(tempRect):
                self.Y -= 2
                break

我建议您使用self.rect中的前两个值作为位置而不是self.X和self.Y,并在需要时修改代码。

答案 1 :(得分:0)

删除XY属性,并使用rect属性存储精灵的位置和大小。应该如何使用它。 Rect个对象有一堆方法和属性可以移动它们,并获得点和边的坐标和位置。

所以Player类的前两个方法可能如下所示:

class Player(pygame.sprite.Sprite):
    def __init__(self):
        self.image = runner[int(floor(currentRunner))]
        self.rect = self.image.get_rect()
        self.rect.topleft = (350, 500)

    def see_on_block(self):
        self.rect.y += 2
        for b in blocks:
            if self.rect.colliderect(b.rect):
                self.rect.y -= 2
                break

最后一种方法可以表达得更紧凑,没有减法:

    def see_on_block(self):
        new_rect = self.rect.move(0, 2)
        if not any(new_rect.colliderect(b.rect) for b in blocks):
            self.rect = new_rect

如果您使用Pygame的精灵群而不是列表,则可以通过调用pygame.sprite.spritecollideany()替换if条件。