2017-08-31 29 views
0

海賊船とブロックが衝突したときにクラッシュ機能を呼び出そうとしていますが、何らかの理由でブロックが画面に当たってクラッシュ機能を呼び出すことがあります。ゲームは衝突すると終了しますが、ブロックが画面の特定の部分に当たったときにゲームはランダムに終了します。衝突検出シンプルゲーム - Pygame

import pygame 
import time 
from pygame.locals import * #all the imports I use 
import random 

pygame.init() # initialise pygame 

display_width = 1500 
display_height = 700 

black = (0,0,0) 
white = (255,255,255) 
red = (255,0,0) 


gameDisplay = pygame.display.set_mode((display_width,display_height)) #creates the window 
pygame.display.set_caption('A bit Shipy') 
clock = pygame.time.Clock() 

shipIMG = pygame.image.load('ship.png') 
ship_width = 50 
ship_height = 50 # in pixels 

def things_dodged(count): 
    font = pygame.font.SysFont(None, 25) 
    text = font.render("Dodged: "+str(count), True, black) 
    gameDisplay.blit(text, (0,0)) 

def things(thingx, thingy, thingw, thingh, color): #the scrolling object 
    pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh]) 

def ship(x,y): #ship function 
    gameDisplay.blit(shipIMG,(x,y)) 



def text_objects(text, font): 
    textSurface = font.render(text, True, red) #game over text 
    return textSurface, textSurface.get_rect() 


def message_display(text): 
    largeText = pygame.font.Font('freesansbold.ttf',200) 
    TextSurf, TextRect = text_objects(text, largeText) 
    TextRect.center = ((display_width/2),(display_height/2)) #creates the text 
    gameDisplay.blit(TextSurf, TextRect) 

    pygame.display.update() 
    time.sleep(2) #Game over text lasts two seconds 

    game_loop() #runs the game again afterwards 

def crash(): 
    message_display('You Sunk!') #when the player goes off screen or hits an object 




#x = (display_width * 0.45) 
#y = (display_height * 0.8) 

def game_loop(): #the main game loop 
    x = (display_width * 0.45) 
    y = (display_height * 0.8) 
    x_change = 0 
    y_change = 0 

    thing_startx = display_width 
    thing_starty = random.randrange(0, display_height) #enemy object 
    thing_speed = 7 
    thing_width = 50 
    thing_height = 50 

    dodged = 0 


    gameExit = False 

    while not gameExit: 

     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       gameExit = True 

      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_LEFT: 
        x_change = -5 
       elif event.key == pygame.K_RIGHT: 
        x_change = 5 

      if event.type == pygame.KEYUP: 
       if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: #changes x or y dependant on key pressed 
        x_change = 0 

      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_UP: 
        y_change = -5 

       elif event.key == pygame.K_DOWN: 
        y_change = 5 

      if event.type == pygame.KEYUP: 
       if event.key == pygame.K_UP or event.key == pygame.K_DOWN: 
        y_change = 0 
     x += x_change 
     y += y_change 



     gameDisplay.fill((0,0,155)) #blue background 


     things(thing_startx, thing_starty, thing_width, thing_height, black) #calls the thing function 
     thing_startx -= thing_speed #moves the block left 
     ship(x,y) 
     things_dodged(dodged) 


     if x > display_width - ship_width or y > display_height-ship_height or x < 0 or y < 0: 
      crash() 

     if thing_startx < 0: 
      thing_startx = display_width 
      thing_starty = random.randrange(0, display_height) 
      dodged = dodged + 1 

     #here is where the collision detection starts:  
     if x < thing_startx+thing_width: 
      print(' y crossover 1') 

      if y >= thing_starty and y < thing_starty + thing_height or y+ship_height > thing_starty and y + ship_height < thing_starty +thing_height: 
       print (' x crossover 2') 
       crash() 

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



game_loop() 
pygame.quit() 
quit() 
+0

「まだ何らかの理由でブロックが画面にヒットするとクラッシュ関数を呼び出します」あなたのコードのI'ts部分: '場合のx> DISPLAY_WIDTH - ship_widthまたはy> display_height-ship_heightまたはx <0のまたはy <0: crash() ' –

+0

船が画面から外れないようにするためのコードです。しかし、敵のスクロールブロックがクラッシュする画面に当たった場合、それを修正する方法はありますか? –

答えて

0

あなたと同じような結託テストが正しくないように見えます。 代替バージョンがあります。

#if x > display_width - ship_width or y > display_height - ship_height or x < 0 or y < 0: 
    if (x + ship_width/2 > display_width or # right edge 
     x - ship_width/2 < 0 or    # left edge 
     y + ship_height/2 > display_height or # top 
     y - ship_width/2 < 0):     # bottom 
     crash() 

船をオブジェクト(クラス)として導入することを検討する必要があります。

x軸上のオブジェクトとあなたの共謀の検出が唯一のことは、船の残っているinfファイルをチェックしている

#here is where the collision detection starts: 
    if (x > thing_startx - thing_width/2 and 
     x < thing_startx + thing_width/2): 
     print(' y crossover 1') 

たぶん、あなたは船との事を表現するために、組み込みのスプライトを使用する必要があります。 https://www.pygame.org/docs/ref/sprite.html#pygame.sprite

これらはオブジェクトであり、他のスプライトとの衝突を検出する機能を持っています。 https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.collide_rect

+0

国境との船の衝突は問題ありません。それは、船と避けるべき黒い正方形である「物」(あまり名づけられていない)との衝突です。しかし、衝突は船舶ではうまくいきません。 –