Pygame - 如何阻止图像离开屏幕边缘?

时间:2014-11-12 22:12:09

标签: python pygame 2d collision detection

当鼠标悬停在窗口边缘时,一段jetfighterx离开屏幕,这会导致狼蛛一旦重新生成窗口顶部就会不时爆炸,我怎么能阻止它发生(没有使用类)?

代码:

import pygame, sys, pygame.mixer
from pygame.locals import *
import random 
pygame.init()

bif = "space.jpg"
jf = "spacefightersprite.png"
enemy = "TarantulaSpaceFighter.png"

laser = pygame.mixer.Sound("LaserBlast.wav")
explosionsound = pygame.mixer.Sound("Explosion.wav") 
screen = pygame.display.set_mode((1000,900),0,32)
caption = pygame.display.set_caption("Jet Fighter X") 
background = pygame.image.load(bif).convert()

jetfighterx = pygame.image.load(jf)
jetfighterx = pygame.transform.scale(jetfighterx, (400,400)) 
tarantula = pygame.image.load(enemy)
tarantula = pygame.transform.scale(tarantula, (100,100)) 
laserblast = pygame.image.load("C:\Python27\laser.png")
explosion=pygame.image.load("C:\Python27\explosion.png")
explosion=pygame.transform.scale(explosion, (150,150))

ex,ey = 450,0
movex,movey = 0,0
clock = pygame.time.Clock()
speed = 300
shoot_y = 0
laser_fired = False
collision = False
alive = True 
explo_timer = 25

while True:
    pygame.mouse.set_visible(False) 
    mx,my = pygame.mouse.get_pos() 
    jetfighterx_rect = jetfighterx.get_rect(center=(mx, my))
    jetfighterx_rect = jetfighterx_rect.inflate(-200,-200) 
    tarantula_rect = tarantula.get_rect(center=(ex, ey))
    tarantula_rect = tarantula_rect.inflate(-180,-200) 
    # Check for player inputs
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE or event.key == K_q:
                sys.exit() 
        if event.type == MOUSEBUTTONDOWN:
            laser_fired = True
            laser.play()
            shoot_y = my-200
            shoot_x = mx-16

    # Update Game 
    milli = clock.tick()
    seconds = milli/1000. 
    dmy = seconds * speed
    ey += dmy

    if ey > 900:
        explo_timer = 25 
        collision = False
        alive = True 
        ey = 0
        ex = random.randint(50,900)
    if laser_fired:
        shoot_y -= 10
        if shoot_y < 0:
            laser_fired = False
        else:
            laserblast_rect = laserblast.get_rect(center=(shoot_x, shoot_y)) 
            if laserblast_rect.colliderect(tarantula_rect):
                explosionsound.play()
                collision = True
                alive = False 

    if jetfighterx_rect.colliderect(tarantula_rect) and alive:
        explosionsound.play()
        collision = True
        alive = False 

  # Draw on screen
    screen.blit(background, (0,0))
    screen.blit(jetfighterx,(mx-200,my-200))
    if not collision:
        screen.blit(tarantula, (ex, ey))
    elif collision:
        explo_timer-=2
        if explo_timer > 0 and alive == False:
            screen.blit(explosion, (ex, ey-50))
    if laser_fired:
        screen.blit(laserblast, (shoot_x, shoot_y))

    pygame.display.update()

1 个答案:

答案 0 :(得分:0)

只需添加一个不允许战斗机在边框x像素内移动的限制。

假设你的战斗机中心的x,y坐标是jetfighter_x,jetfighter_y(你需要将变量名改为你的代码)然后写下这样的东西:

LBuffer = 16
RBuffer = 1000 - 16
TBuffer = 900 - 16
BBuffer = 16

if jetfighter_x > RBuffer:
    jetfighter_x = RBuffer

if jetfighter_x < LBuffer:
    jetfighter_x = LBuffer

if jetfighter_y > TBuffer:
    jetfighter_y = TBuffer

if jetfighter_y < BBuffer:
    jetfighter_y = BBuffer

这样可以防止船的中心距边缘16个像素。显然,你需要调整它以适应你的船的大小。 (边的缓冲区是图像的宽度/ 2。顶部和底部的缓冲区分别是图像的高度/ 2)。