2016-12-09 6 views
0

Я пытаюсь смоделировать столкновение в прототипе прототипа бегуна. проблема заключается в том, что логично останавливать движение персонажа и взаимодействовать с объектом.Pygame Collision взаимодействие логическое

Я пробовал так много вещей, но ничего.

вот мой код: проблема в конце концов

import pygame 
import os 

pygame.init() 
screen = pygame.display.set_mode((400,300)) 
pygame.display.set_caption("shield hacking") 
JogoAtivo = True 
GAME_BEGIN = False 
run =True 
jumping =True; 
# Speed in pixels per frame 
speedX = 0 
speedY = 0 
cordX = 10; 
cordY = 150; 
groundX=0; 
groundY=200; 
verify = open('verify_continue.txt' , "r"); 
sound_trigger=False 



def loadCoords(): 
    f1 = open("coords.txt", "r") 
    text=f1.read() 
    num_list=text.split() 
    print (num_list) 
    cordX=num_list[0] 
    print(cordX) 
    cordY=num_list[1] 
    f1.close() 
    return cordX,cordY 


def copiaArquivo(cordX, cordY): 
    x=str(cordX); 
    y=" "+str(cordY); 
    f1 = open('coords.txt', "w") 
    f1.write(x); 
    f2 = open('coords.txt', "w") 
    f2.seek(2) 
    f2.write(y); 
    f1.close() 
    f2.close() 


def draw(): 
    screen.fill((0, 0, 0)) 
    ground = pygame.draw.rect(screen, (0, 255, 0), (groundX, groundY,400, 10)) 
    square = pygame.draw.rect(screen, (255, 0, 0), (cordX, cordY ,50, 50)) 
    enemy = pygame.draw.rect(screen, (255, 0, 155), (200, 150 ,50, 50)); 
    pygame.display.flip(); 


""" SCREEN(press START) """ 
font = pygame.font.SysFont(None, 55); 
text = font.render("Press A to Start", 1, (255, 0, 0)); 
screen.blit(text,(50,20)); 
pygame.display.update(); 


while JogoAtivo: 
    for evento in pygame.event.get(): 
     print(evento) 

    #verifica se o evento que veio eh para fechar a janela 
     if evento.type == pygame.QUIT: 
       JogoAtivo = False 
       copiaArquivo(cordX, cordY); 
       pygame.quit(); 
     if evento.type == pygame.KEYDOWN: 
      if evento.key == pygame.K_a: 
        print('GAME BEGIN') 
        GAME_BEGIN = True 
        sound_trigger = False 
        if verify.read()=="S": 
         cordX,cordY=loadCoords(); 
         cordX = float(cordX); 
         cordY = float(cordY); 
        pygame.mixer.init(); 
        if sound_trigger == True: 
         sound1 = pygame.mixer.Sound('GunsNRoses_ParadiseRemix_xD.wav'); 
         chan1 = pygame.mixer.find_channel() 
         chan1.queue(sound1); 
      if evento.key == pygame.K_LEFT: 
        speedX=-0.025 
        run= True; 
      if evento.key == pygame.K_RIGHT: 
        speedX=0.025 
        run= True; 
      if evento.key == pygame.K_SPACE: 
        print("x ", cordX) 
        print("y " ,cordY) 
        speedY=-0.3 
        jumping= True; 
        if sound_trigger == True: 
         sound2 = pygame.mixer.Sound('MMX2_SE_00019.wav'); 
         chan2 = pygame.mixer.find_channel(); 
         chan2.queue(sound2); 
     if evento.type == pygame.KEYUP: 
      if evento.key == pygame.K_SPACE: 
        speedY=+0.2 




    if GAME_BEGIN: 
     draw(); 
     if run == True: 
      cordX+=speedX 
     if jumping == True: 
      cordY+=speedY 

    """square = pygame.draw.rect(screen, (255, 0, 0), (cordX, cordY ,50, 50)) 
    enemy = pygame.draw.rect(screen, (255, 0, 155), (200, 150 ,50, 50));""" 


    """ground detection""" 
    if cordY +50>= groundY: 
      speedY=0 

#PROBLEM HERE: 
      """i think to use colliderRect() method could be easier""" 

    if cordX+50 <=200 or cordX >=250: #200 and 250 == object X coords 
     cordY+=speedY; 
     cordX+=speedX; 
    elif cordX + 50 >=200 and cordX <=250 and cordY >=150: 
      speedX=0; 

    elif cordX + 50 >=200 and cordX <=250 and cordY + 50 >=150: 
      speedY=0; 

    """if cordX + 50 >=200 and cordX +50 <=250 and cordY >= 100: 
      speedY=0;""" 
+0

использование 'pygame.Rect()', чтобы сохранить положение и размер объекта. И тогда вы можете использовать 'rect.x',' rect.y', 'rect.right' (=' rect.x + rect.width'), 'rect.left',' rect.top', rect.bottom (= 'rect.y + rect.height') и т. д. и' rect.colliderect (other_rect) ',' rect.collidepoint (mouse_position) 'и' pygame.draw.rect (screen, (255,0,0), прямоугольник) '. BTW: вам не нужно присваивать переменной 'pygame.draw.rect()' переменную, если вы не используете эту переменную позже. – furas

+0

BTW: Python не нужен ';' – furas

+0

почему вы открываете один и тот же файл два раза, чтобы написать два элемента - один раз открыть и использовать 'write()' два раза. – furas

ответ

0

Ответил:

import pygame 
import os 

pygame.init() 
screen = pygame.display.set_mode((400,300)) 
pygame.display.set_caption("shield hacking") 
JogoAtivo = True 
GAME_BEGIN = False 
run =True 
jumping =True; 
# Speed in pixels per frame 
speedX = 0 
speedY = 0 
cordX = 10; 
cordY = 150; 
groundX=0; 
groundY=200; 
verify = open('verify_continue.txt' , "r"); 


def loadCoords(): 
    f1 = open("coords.txt", "r") 
    text=f1.read() 
    num_list=text.split() 
    print (num_list) 
    cordX=num_list[0] 
    print(cordX) 
    cordY=num_list[1] 
    f1.close() 
    return cordX,cordY 


def copiaArquivo(cordX, cordY): 
    x=str(cordX); 
    y=" "+str(cordY); 
    f1 = open('coords.txt', "w") 
    f1.write(x); 
    f2 = open('coords.txt', "w") 
    f2.seek(2) 
    f2.write(y); 
    f1.close() 
    f2.close() 


def draw(): 
    screen.fill((0, 0, 0)) 
    ground = pygame.draw.rect(screen, (0, 255, 0), (groundX, groundY,400, 10)) 
    square = pygame.draw.rect(screen, (255, 0, 0), (cordX, cordY ,50, 50)) 
    enemy = pygame.draw.rect(screen, (255, 0, 155), (200, 150 ,50, 50)); 
    pygame.display.flip(); 


""" SCREEN(press START) """ 
font = pygame.font.SysFont(None, 55); 
text = font.render("Press A to Start", 1, (255, 0, 0)); 
screen.blit(text,(50,20)); 
pygame.display.update(); 


while JogoAtivo: 
    for evento in pygame.event.get(): 
     print(evento) 

    #verifica se o evento que veio eh para fechar a janela 
     if evento.type == pygame.QUIT: 
       JogoAtivo = False 
       copiaArquivo(cordX, cordY); 
       pygame.quit(); 
     if evento.type == pygame.KEYDOWN: 
      if evento.key == pygame.K_a: 
        print('GAME BEGIN') 
        GAME_BEGIN = True 
        if verify.read()=="S": 
         cordX,cordY=loadCoords(); 
         cordX = float(cordX); 
         cordY = float(cordY); 
        pygame.mixer.init(); 
        sound1 = pygame.mixer.Sound('GunsNRoses_ParadiseRemix_xD.wav'); 
        chan1 = pygame.mixer.find_channel() 
        chan1.queue(sound1); 
      if evento.key == pygame.K_LEFT: 
        speedX=-0.006 
        run= True; 
      if evento.key == pygame.K_RIGHT: 
        speedX=0.006 
        run= True; 
      if evento.key == pygame.K_SPACE: 
        print("x ", cordX) 
        print("y " ,cordY) 
        speedY=-0.3 
        jumping= True; 
        sound2 = pygame.mixer.Sound('MMX2_SE_00019.wav'); 
        chan2 = pygame.mixer.find_channel(); 
        chan2.queue(sound2); 
     if evento.type == pygame.KEYUP: 
      if evento.key == pygame.K_SPACE: 
        speedY=+0.2 




    if GAME_BEGIN: 
     draw(); 
     if run == True: 
      cordX+=speedX 
     if jumping == True: 
      cordY+=speedY 

    """square = pygame.draw.rect(screen, (255, 0, 0), (cordX, cordY ,50, 50)) 
    enemy = pygame.draw.rect(screen, (255, 0, 155), (200, 150 ,50, 50));""" 


    """ground detection""" 
    if cordY +50>= groundY: 
      speedY=0 

    if cordX + 50 >=200 and cordX +50 <=250 and cordY < 100: 
      speedX=0; 

    if cordX + 50 >=200 and cordX +50 <=250 and cordY >= 100: 
      speedY=0;