简体   繁体   English

测试pygame精灵碰撞

[英]Testing for pygame sprite collision

I have been going through some pygame tutorials, and I have developed my own code based off of these tutorials. 我一直在浏览一些pygame教程,并根据这些教程开发了自己的代码。 It is as follows: 它如下:

#!/usr/bin/python

import pygame, sys
from pygame.locals import *

size = width, height = 320, 320
clock = pygame.time.Clock()

xDirection = 0
yDirection = 0
xPosition = 32
yPosition = 256

blockAmount = width/32

pygame.init()
screen = pygame.display.set_mode(size)
screen.fill([0, 155, 255])
pygame.display.set_caption("Mario Test")
background = pygame.Surface(screen.get_size())

mainCharacter = pygame.sprite.Sprite()
mainCharacter.image = pygame.image.load("data/character.png").convert()
mainCharacter.rect = mainCharacter.image.get_rect()
mainCharacter.rect.topleft = [xPosition, yPosition]
screen.blit(mainCharacter.image, mainCharacter.rect)

grass = pygame.sprite.Sprite()
grass.image = pygame.image.load("data/grass.png").convert()
grass.rect = grass.image.get_rect()
for i in range(blockAmount):
    blockX = i * 32
    blockY = 288
    grass.rect.topleft = [blockX, blockY]
    screen.blit(grass.image, grass.rect)

grass.rect.topleft = [64, 256]  
screen.blit(grass.image, grass.rect.topleft )

running = False
jumping = False 
falling = False
standing = True

jumpvel = 22
gravity = -1

while True:

    for event in pygame.event.get():
        if event.type == KEYDOWN and event.key == K_ESCAPE:
            pygame.quit()
            sys.exit()
        elif event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                running = True
                xRun = -5
            elif event.key == K_RIGHT:
                running = True
                xRun = 5
            elif event.key == K_UP or event.key == K_SPACE:
                jumping = True
        elif event.type == KEYUP:
            if event.key == K_LEFT or event.key == K_RIGHT:
                running = False


    if running == True:
        xPosition += xRun

    if mainCharacter.rect.right >= width:
        xPosition = xPosition - 10
        print "hit"
        running = False
    elif mainCharacter.rect.left <= 0:
        xPosition = xPosition + 10
        print "hit"
        running = False

    screen.fill([0, 155, 255])

    for i in range(blockAmount):
        blockX = i * 32
        blockY = 288
        grass.rect.topleft = [blockX, blockY]
        screen.blit(grass.image, grass.rect)

    grass.rect.topleft = [64, 64]   
    screen.blit(grass.image, grass.rect.topleft )


    if jumping:
        yPosition -= jumpvel
        print jumpvel
        jumpvel += gravity
        if jumpvel < -22:
            jumping = False
        if mainCharacter.rect.bottom == grass.rect.top:
            jumping = False

    if not jumping:
        jumpvel = 22


    mainCharacter.rect.topleft = [xPosition, yPosition]
    screen.blit(mainCharacter.image,mainCharacter.rect)

    clock.tick(60)
    pygame.display.update() 

So I have figured out how to make my dude jump, however I have placed a block in the sky and tried to do this: 所以我已经想出了如何让我的家伙跳,但是我已经在天空中放置了一块并试图这样做:

if mainCharacter.rect.bottom == grass.rect.top:
    jumping = False

to attempt to make the character stop jumping. 试图使角色停止跳跃。 Is there a built in function to test this or perhaps a way that works in my case. 是否有内置函数来测试这个或者可能是一种在我的情况下有效的方法。 This doesn't work by the way, and I can't seem to figure out how to do it. 顺便说一句,这不起作用,我似乎无法弄清楚如何做到这一点。 Any help is greatly appreciated :) 任何帮助是极大的赞赏 :)

The problem with your code is that your character is moving more than one pixel each step. 代码的问题在于您的角色每步移动多个像素。 For example, when moving at maximum velocity, your character moves 22 pixels each step. 例如,当以最大速度移动时,角色每步移动22个像素。 So if he's 10 pixels above the grass one step, he'll be 12 pixels below the grass on the next step. 因此,如果他在草地上方10个像素一步,他将在下一步草地下方 12像素。 To fix this, you test to see whether the character is touching or below the grass, like so: 要解决此问题,您需要测试角色是否在草地上或在草地下面,如下所示:

if mainCharacter.rect.bottom <= grass.rect.top:
    jumping = False

Also, Pygame does have a built-in function to test collision detection: rect1.colliderect(rect2) will return True if rect1 and rect2 are colliding, and will return False otherwise. 另外,Pygame确实有一个内置函数来测试碰撞检测:如果rect1和rect2发生碰撞, rect1.colliderect(rect2)将返回True,否则返回False。 However, it is slightly more costly than the aforementioned code, and it may cause the same problem if your character is moving so fast that he passes through the grass completely. 然而,它比上述代码稍微昂贵,如果你的角色移动速度太快以至于他完全穿过草地,它可能会导致同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM