当海盗船和街区发生碰撞时,试图调用崩溃功能,但由于某种原因,该块将会到达屏幕并调用崩溃功能。游戏确实在碰撞时结束,但是当游戏块碰到屏幕的某些部分时,游戏将随机结束。
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 :(得分: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轴上的物体的碰撞检测仅检查该块是否留在船上:
#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')
也许你应该使用内置精灵来代表船只和街区:sprite
它们是对象,具有检测与其他精灵共谋的功能:collide_rect