1

我一直在阅读一些 pygame 教程,并根据这些教程开发了自己的代码。如下:

#!/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() 

所以我想出了如何让我的家伙跳起来,但是我在天空中放置了一个块并尝试这样做:

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

试图让角色停止跳跃。是否有内置函数来测试这个,或者可能是一种适用于我的情况的方法。顺便说一句,这不起作用,我似乎不知道该怎么做。任何帮助是极大的赞赏 :)

4

1 回答 1

3

您的代码的问题是您的角色每一步移动的像素不止一个像素。例如,当以最大速度移动时,您的角色每步移动 22 个像素。因此,如果他一步高于草 10 像素,那么下一步他将低于草 12 像素。为了解决这个问题,您可以测试角色是接触草还是在草下,如下所示:

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

此外,Pygame 确实有一个内置函数来测试碰撞检测: rect1.colliderect(rect2)如果 rect1 和 rect2 发生碰撞,则返回 True,否则返回 False。但是,它比前面提到的代码稍微贵一点,如果你的角色移动得太快以至于他完全穿过草丛,它可能会导致同样的问题。

于 2013-06-06T17:51:51.257 回答