子弹长度调整不了
目前只完成一些
但飞船子弹完成了
船类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 update(self):
'''调整飞船位置'''
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
self.rect.x = self.x
def blitme(self):
'''在特定的位置显示飞船'''
self.screen.blit(self.image,self.rect)
设置类
class Settings:
'''存储游戏设置的类'''
def __init__(self):
'''初始化游戏的设置'''
# 屏幕设置
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
#飞船的设置
self.ship_speed = 2
#子弹设置
self.bullet_speed = 3.0
self.bullet_height = 1
self.bullet_width = 3
self.bullet_color = (60, 60, 60)
self.bullets_allowed = 10
总类
import pygame
import sys
from settings import Settings
from ship import Ship
from bullet import Bullet
class AlienInvasion:
'''管理游戏资源和行为的类'''
def __init__(self):
"""初始化游戏并创建游戏资源"""
pygame.init()
self.clock = pygame.time.Clock()
self.settings = Settings()
self.screen = pygame.display.set_mode(
(self.settings.screen_width,self.settings.screen_height))
self.scree = pygame.display.set_mode((1200,800))
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
self.bullets = pygame.sprite.Group()
# 设置背景色
self.bg_color = (26,160,75)
def run_game(self):
'''开始游戏的主循环'''
while True:
self._check_events()
self.ship.update()
self.bullets.update()
#删除已消失的子弹
for bullet in self.bullets.copy():
if bullet.rect.bottom <= 0:
self.bullets.remove(bullet)
print(len(self.bullets))
self._update_screen()
self.clock.tick(65)
# 看键盘和鼠标指示
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# 每次循环时都重绘屏幕
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
# 让玩家看见东西
pygame.display.flip()
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_0:
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 _update_screen(self):
'''更新图像'''
self.screen.fill(self.settings.bg_color)
for bullet in self.bullets.sprites():
bullet.draw_bullet()
self.ship.blitme()
pygame.display.flip()
def _fire_bullet(self):
'''创建一个子弹,并加入编组bullets'''
if len(self.bullets) < self.settings.bullets_allowed:
new_bullet = Bullet(self)
self.bullets.add(new_bullet)
if __name__ == '__main__':
# 创建游戏示例进行游戏
ai = AlienInvasion()
ai.run_game()
子弹类
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.screen_height)
self.rect.midtop = ai_game.ship.rect.midtop
#存储浮点数表示的子弹位置
self.y = float(self.rect.y)
def update(self):
'''向上移动子弹'''
#更新子弹的准确位置
self.y -= self.settings.bullet_speed
self.rect.y = self.y
def draw_bullet(self):
'''在屏幕上绘制子弹'''
pygame.draw.rect(self.screen,self.color,self.rect)
具体情况请看
https://www.bilibili.com/video/BV1nMYCeAEo3?t=3.6
新情况
https://www.bilibili.com/video/BV1wMe7ewEho?t=10.4
修改了参数还是不行
把子弹长度改成0还是很长