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

2024-09-23 22:28:04 发布

您现在位置:Python中文网/ 问答频道 /正文

当鼠标悬停在窗口边缘时,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()

Tags: rectimageeventfalsetrueifscreenpygame
1条回答
网友
1楼 · 发布于 2024-09-23 22:28:04

只需添加一个限制,不允许战斗机在边界的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)。在

相关问题 更多 >