2013-06-06 3 views
1

파이 게임 튜토리얼을 다뤘습니다.이 튜토리얼을 기반으로 자신 만의 코드를 개발했습니다. 다음과 같이이다 :파이 게임 스프 라이트 충돌 테스트

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

하는을 시도하는 방법을 그 캐릭터는 점프를 멈춘다. 이 기능을 테스트하거나 필자의 경우 작동하는 방법이 내장되어 있습니다. 이 방법으로는 효과가 없으므로 어떻게해야하는지 알 수 없습니다. 어떤 도움을 크게 주시면 감사하겠습니다 :)

답변

3

코드 문제는 캐릭터가 각 단계마다 두 개 이상의 픽셀을 움직이고 있다는 것입니다. 예를 들어, 최대 속도로 움직일 때, 캐릭터는 각 단계마다 22 픽셀 씩 움직입니다. 따라서 그가 잔디 한 단계 위의 10 픽셀이라면 12 픽셀이됩니다 다음 단계의 잔디. 이 문제를 해결하려면, 당신과 같이, 캐릭터가 만지거나 잔디 아래에 있는지 여부를 확인하기 위해 테스트 :

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

이, 파이 게임이 충돌 탐지 테스트 할 수있는 내장 기능을 가지고 않습니다 rect1 경우 rect1.colliderect(rect2)가 TRUE를 반환합니다 rect2가 충돌하고 그렇지 않으면 False를 반환합니다. 그러나 이것은 전술 한 코드보다 약간 더 비쌉니다. 캐릭터가 너무 빨리 움직여 잔디를 완전히 지나치면 같은 문제가 발생할 수 있습니다.