python初学者问题:外星人游戏,飞船被限制在屏幕内移动,如何控制边缘移动距离?
if self.moving_right and self.rect.right<self.screen_rect.right:
self.center+=self.ai_settings.ship_speed_factor
上述代码确保飞船只在屏幕内移动,测试没有问题,
疑惑的是,从代码来看,只有当飞船外接矩形的右坐标小于屏幕右边缘坐标时,飞船的X坐标才会增加
但是,如果当飞船距离右边缘的坐标差仅为1时,即假设飞船右坐标为1199,而屏幕右边缘坐标为1200,此时应该可以满足飞船向右移动,且移动距离为设置的速度,如果设置的速度为5,即self.ai_settings.ship_speed_factor=5,则飞船应该向右移动5个值,但实际上飞船只向右移动1个值,是哪段代码保证这样的运行结果?
希望各位指点迷津,指教一下,实在是想不通,万分感谢!
self.center应该是个属性,而不仅仅是字段吧
你去查它的定义,看是不是在setter里处理过了
其实我很好奇你是怎么确定“飞船只向右移动1个值”的?如果你把飞船的速度调至50,就可以看到,在你说的这种情况下,飞船明显有部分超出了窗口。
所以在你举的例子里,1个像素实在是肉眼无法看到。还有一种可能就是,屏幕的宽度正好等于速度的整数倍,所以没有出现你说的这种情况。
为方便热心朋友解惑,放如下教材原代码,
settings.py
class Settings:
"""A class to store all settings for Alien Invasion."""
def __init__(self):
"""Initialize the game's settings."""
# Screen settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = 130, 21, 160
# Ship settings
self.ship_speed = 5
# Bullet settings
self.bullet_speed = 1.0
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = (60, 60, 60)
self.bullets_allowed = 3
ship.py
import pygame
class Ship:
"""A class to manage the ship."""
def __init__(self, ai_game):
"""Initialize the ship and set its starting position."""
self.screen = ai_game.screen
self.settings = ai_game.settings
self.screen_rect = ai_game.screen.get_rect()
# Load the ship image and get its rect.
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
# Start each new ship at the bottom center of the screen.
self.rect.midbottom = self.screen_rect.midbottom
# Store a decimal value for the ship's horizontal position.
self.x = float(self.rect.x)
# Movement flags
self.moving_right = False
self.moving_left = False
def update(self):
"""Update the ship's position based on movement flags."""
# Update the ship's x value, not the rect.
if self.moving_right and self.rect.right < self.screen_rect.right:
self.x += self.settings.ship_speed
if self.moving_left and self.rect.left > 0:
self.x -= self.settings.ship_speed
# Update rect object from self.x.
self.rect.x = self.x
def blitme(self):
"""Draw the ship at its current location."""
self.screen.blit(self.image, self.rect)
alien_invasion.py
import sys
import pygame
sys.path.append('.\invasion\')
from settings import Settings
from ship import Ship
from bullet import Bullet
class AlienInvasion:
"""管理游戏资源和行为的类"""
def __init__(self):
"""初始化游戏并创建游戏资源。"""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
self.settings.screen_width = self.screen.get_rect().width
self.settings.screen_height = self.screen.get_rect().height
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
self.bullets = pygame.sprite.Group()
def run_game(self):
"""开始游戏主循环。"""
while True:
self._check_events()
self.ship.update()
self._update_bullets()
self._update_screen()
def _check_events(self):
"""响应鼠标和按键事件。"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
self._check_keydown_events(event)
elif event.type == pygame.KEYUP:
self._check_keyup_events(event)
def _check_keydown_events(self, event):
"""响应按键。"""
if event.key == pygame.K_RIGHT:
self.ship.moving_right = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif event.key == pygame.K_q:
sys.exit()
elif event.key == pygame.K_SPACE:#❷
self._fire_bullet()
def _check_keyup_events(self, event):
"""响应松开。"""
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
def _fire_bullet(self):
"""创建一颗子弹,并将其加入编组bullets中。"""
if len(self.bullets) < self.settings.bullets_allowed:
new_bullet = Bullet(self)
self.bullets.add(new_bullet)
def _update_bullets(self):
"""Update position of bullets and get rid of old bullets."""
# 更新子弹位置。
self.bullets.update()
# 删除消失的子弹。
for bullet in self.bullets.copy():
if bullet.rect.bottom <= 0:
self.bullets.remove(bullet)
def _update_screen(self):
"""更新屏幕上的图像,并切换到新屏幕。"""
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
for bullet in self.bullets.sprites():#❺
bullet.draw_bullet()
pygame.display.flip()
if name == 'main':
# 创建游戏实例并运行游戏。
ai = AlienInvasion()
ai.run_game()