在没有添加左右的活动范围时,还可以移动,为什么添加左右范围后就无法移动了?
# alien_invasion.py
import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
"""管理游戏资源和行为的类"""
def __init__(self):
"""初始化游戏并创建游戏资源"""
pygame.init()
self.settings = Settings()
# self.screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN)
self.screen = pygame.display.set_mode((self.settings.screen_width,self.settings.screen_height))
# self.settings.screen_width = self.screen.get_rect().width
# self.settings.screen_height = self.screen.get_rect().height
pygame.display.set_caption("AlienInvasion")
self.ship = Ship(self)
def run_game(self):
"""开始游戏的主循环"""
while True:
self._check_events()
self.ship.updata()
self._updata_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()
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
# 向右移动飞船
self.ship.rect.x += 1
def _updata_screen(self):
"""更新屏幕上的图像,并切换到新的屏幕"""
# 每次循环时都重绘屏幕
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
# 让最近绘制的屏幕可见
pygame.display.flip()
if __name__ == '__main__':
# 创建游戏实例并运行游戏
ai = AlienInvasion()
ai.run_game()
# bullet.py
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""管理飞船所发射子弹的类"""
def __init__(self,ai_game):
"""在飞船所发射子弹的类"""
super().__init__()
self.screen = ai_game.screen
self.settings = ai_game.settings
self.color = self.settings.bullet_color
# 在(0,0)处创建一个表示子弹的矩形,在设置正确的位置
self.rect = pygame.Rect(0,0,self.settings.bullet_width,self.settings.bullet_height)
self.rect.midtop = ai_game.ship.rect.midtop
# 存储用小数表示的子弹位置
self.y = float(self.rect.y)
# settings.py
class Settings:
"""存储游戏《外星人入侵》中所有设置的类"""
def __init__(self):
"""初始化游戏的设置"""
# 屏幕设置
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230,230,230)
# 飞船设置
self.ship_speed = 1.5
#子弹设置
self.bullet_spend = 1.0
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = (60,60,60)
# ship.py
import pygame
class Ship:
"""管理飞船的类"""
def __init__(self,ai_game):
"""初始化飞船并设置其初始位置"""
self.screen = ai_game.screen
self.settings = ai_game.settings
self.screen_rect = ai_game.screen.get_rect()
# 加载飞船图像并获取其外接矩形
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
# 对于每艘新飞船,都将其放在屏幕底部的中央
self.rect.midbottom = self.screen_rect.midbottom
# 在飞船的属性x中存储小数值
self.x = float(self.rect.x)
# 移动标志
self.moving_right = False
self.moving_left = False
def updata(self):
"""根据移动标准调整飞船的位置"""
# 更新飞船而不是rect对象的x值
# if self.moving_right:
# 飞船右移不出框
if self.moving_right and self.rect.right < self.screen_rect.right:
self.rect.x += self.settings.ship_speed
# self.rect.x += 1
# if self.moving_left:
# 飞船左移不出框
if self.moving_right and self.rect.left > 0:
self.rect.x -= self.settings.ship_speed
# self.rect.x -= 1
# 根据self.x更新rect对象
self.rect.x += self.x
def blitme(self):
"""在指定位置绘制飞船"""
self.screen.blit(self.image, self.rect)