|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我的飞船右上,右下,左下飞行的同时都可以发射子弹,为什么左上飞行的同时不能发射?
import pygame,sys
from pygame.sprite import Sprite
from pygame.sprite import Group
class Settings():
def __init__(self):
self.screen_width = 800
self.screen_height = 600
self.bg_color = (230, 230, 230)
self.ship_speed_factor = 0.5
self.bullet_speed_factor = 0.6
self.bullet_width = 3.1
self.bullet_height = 5
self.bullet_color = 150, 50, 50
class Bullet(Sprite):
def __init__(self, Settings, screen, ship):
super(Bullet, self).__init__()
self.screen = screen
self.rect = pygame.Rect(0, 0, Settings.bullet_width,Settings.bullet_height)
self.rect.centerx = ship.rect.centerx
self.rect.top = ship.rect.top
self.y = float(self.rect.y)
self.color = Settings.bullet_color
self.speed_factor = Settings.bullet_speed_factor
def update(self):
self.y -= self.speed_factor
self.rect.y = self.y
def draw_bullet(self):
pygame.draw.rect(self.screen, self.color, self.rect)
def update_bullets(bullets):
bullets.update()
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
class Ship():
def __init__(self, ai_sets , screen):
"""初始化飞船并设置其初始位置"""
self.screen = screen
self.image = pygame.image.load('ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
self.ai_sets = ai_sets
self.center=self.screen_rect.centerx
self.y=self.screen_rect.bottom
self.moving_right = False
self.moving_left = False
self.moving_up = False
self.moving_down = False
def blitme(self):
"""在指定位置绘制飞船"""
self.screen.blit(self.image, self.rect)
def updata(self):
if self.moving_right and self.rect.right < self.screen_rect.right:
self.center+= self.ai_sets.ship_speed_factor
if self.moving_left and self.rect.left > 0:
self.center -= self.ai_sets.ship_speed_factor
if self.moving_up and self.rect.bottom > 50:
self.y-= self.ai_sets.ship_speed_factor
if self.moving_down and self.rect.bottom < 600:
self.y+= self.ai_sets.ship_speed_factor
self.rect.centerx=self.center
self.rect.bottom=self.y
def check_keydown_events(event, aisets, screen, ship, bullets):
"""响应按键"""
if event.key == pygame.K_SPACE:
new_bullet = Bullet(aisets, screen, ship)
bullets.add(new_bullet)
elif event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.key == pygame.K_UP:
ship.moving_up = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
elif event.key == pygame.K_DOWN:
ship.moving_down = True
def check_keyup_events(event, ship):
"""响应松开"""
if event.key == pygame.K_RIGHT:
ship.moving_right = False
elif event.key == pygame.K_LEFT:
ship.moving_left = False
elif event.key == pygame.K_UP:
ship.moving_up = False
elif event.key == pygame.K_DOWN:
ship.moving_down = False
def check_events(ai_settings, screen, ship, bullets):
"""响应按键和鼠标事件"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown_events(event, ai_settings, screen, ship, bullets)
elif event.type == pygame.KEYUP:
check_keyup_events(event, ship)
def update_screen(ai_settings, screen, ship,bullets):
screen.fill(ai_settings.bg_color)
for bullet in bullets.sprites():
bullet.draw_bullet()
ship.blitme()
pygame.display.flip()
def run_game():
pygame.init()
ai_set=Settings()
screen=pygame.display.set_mode((ai_set.screen_width,ai_set.screen_height))
ship=Ship(ai_set, screen)
bullets = Group()
while True:
check_events(ai_set, screen, ship, bullets)
ship.updata()
update_bullets(bullets)
update_screen(ai_set, screen, ship, bullets)
run_game() |
|