最近写的一个飞机大战的程序,写到一半有一个已经写好的函数但不能运用,强行运行程序秒退,百思不得其姐
这是程序
主程序
import pygame#模块pygame包含开发游戏所需的功能
from pygame.sprite import Group
from setting import Settings
from ship import Ship
import game_function as gf
def run_game():
#初始化游戏并创建一个屏幕对象
pygame.init()
#初始化背景设置
ai_settings=Settings()
screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
#(1200,800)是一个元组 pygame.display.set_mode()返回一个surface(每个元素都是都是一个surface),显示整个游戏画面
bullets=Group()#创建一个用于储存子弹的编组
aliens=Group()
gf.create_fleet(ai_settings,screen,aliens)
pygame.display.set_caption("Alien Invasion")
ship=Ship(ai_settings,screen)
#开始游戏主循环
while True:
gf.check_events(ai_settings,screen,ship,bullets)
ship.update()
gf.update_bullets(bullets)
gf.update_screen(ai_settings,screen,ship,aliens,bullets)
run_game()
游戏设置
import sys
import pygame
from Bullet import bullet
from alien import Alien
def check_key_down_events(event,ai_settings,screen,ship,bullets):
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
elif event.key==pygame.K_UP:
ship.moving_up=True
elif event.key==pygame.K_DOWN:
ship.moving_down=True
elif event.key==pygame.K_SPACE:
fire_bullet(ai_settings, screen, ship, bullets)
def check_key_up_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 or pygame.K_q:
sys.exit()
elif event.type==pygame.KEYDOWN:
check_key_down_events(event,ai_settings,screen,ship,bullets)
elif event.type==pygame.KEYUP:
check_key_up_events(event,ship)
def update_screen(ai_settings,screen,ship,aliens,bullets):
# 每次循环都重绘屏幕
screen.fill(ai_settings.bg_color)
for bullet in bullets:
bullet.draw_bullet()
ship.blitme()
aliens.draw(screen)
pygame.display.flip()
def update_bullets(bullets):
bullets.update()
for bullet in bullets:
if bullet.rect.bottom < 0:
bullets.remove(bullet)
# 让最近绘制的屏幕可见
pygame.display.flip()
def fire_bullet(ai_settings,screen,ship,bullets):
if len(bullets) <= ai_settings.bullet_allowed:
new_bullet = bullet(ai_settings, screen, ship)
bullets.add(new_bullet)
def create_fleet(ai_settings,screen,aliens):
alien=Alien(ai_settings,screen)
alien_width=alien.rect.width
available_space_x=ai_settings.screen_width-alien_width*2
number_alien_x=int(available_space_x/(2*alien_width))
for alien_number in range(number_alien_x):
alien=Alien(ai_settings,screen)
alien.x=alien_width+2alien_widthalien_number
alien.rect.x=alien.x
aliens.add(alien)
class Settings:
def __init__(self):
"初始化游戏的设置"
#屏幕设置
self.screen_width=1200
self.screen_height=800
self.bg_color=(102,204,255)
self.ship_speed_factor=1.5
#子弹设置
self.bullet_speed_factor=2
self.bullet_width=1
self.bullet_height=15
self.bullet_color=60,60,60
self.bullet_allowed=3
class Settings:
def __init__(self):
"初始化游戏的设置"
#屏幕设置
self.screen_width=1200
self.screen_height=800
self.bg_color=(102,204,255)
self.ship_speed_factor=1.5
#子弹设置
self.bullet_speed_factor=2
self.bullet_width=1
self.bullet_height=15
self.bullet_color=60,60,60
self.bullet_allowed=3
基础
class Settings:
def __init__(self):
"初始化游戏的设置"
#屏幕设置
self.screen_width=1200
self.screen_height=800
self.bg_color=(102,204,255)
self.ship_speed_factor=1.5
#子弹设置
self.bullet_speed_factor=2
self.bullet_width=1
self.bullet_height=15
self.bullet_color=60,60,60
self.bullet_allowed=3
class Settings:
def __init__(self):
"初始化游戏的设置"
#屏幕设置
self.screen_width=1200
self.screen_height=800
self.bg_color=(102,204,255)
self.ship_speed_factor=1.5
#子弹设置
self.bullet_speed_factor=2
self.bullet_width=1
self.bullet_height=15
self.bullet_color=60,60,60
self.bullet_allowed=3
飞船
import pygame
class Ship():
def __init__(self,ai_settings,screen):
self.screen=screen
self.ai_setting=ai_settings
#加载飞船并获取其外接矩形
self.image=pygame.image.load("imagesšipb.bmp")
self.rect=self.image.get_rect()
self.screen_rect=self.screen.get_rect()
#将飞船放在屏幕底部中央
self.rect.centerx=self.screen_rect.centerx
self.rect.bottom=self.screen_rect.bottom
self.center_x=float(self.rect.centerx)
self.center_y=float(self.rect.centery)
#移动标志
self.moving_right=False
self.moving_left= False
self.moving_up=False
self.moving_down=False
def update(self):
if self.moving_right and self.rect.right<self.screen_rect.right:
self.center_x+=self.ai_setting.ship_speed_factor
if self.moving_left and self.rect.left>0:
self.center_x-=self.ai_setting.ship_speed_factor
if self.moving_up and self.rect.top>0:
self.center_y-=self.ai_setting.ship_speed_factor
if self.moving_down and self.rect.bottom<self.screen_rect.bottom:
self.center_y+=self.ai_setting.ship_speed_factor
self.rect.centerx=self.center_x
self.rect.centery=self.center_y
def blitme(self):
"在指定位置绘制飞船"
self.screen.blit(self.image,self.rect)
外星人
import pygame
from pygame.sprite import Sprite
class Alien(Sprite):
def __init__(self,ai_settings,screen):
super().__init__()
self.screen=screen
self.ai_settings=ai_settings
self.image=pygame.image.load("images/alien.bmp")
self.rect=self.image.get_rect()
self.rect.x=self.rect.width
self.rect.y=self.rect.height
self.x=float(self.rect.x)
def blitme(self):
self.screen.blit(self.image,self.rect)
子弹
rt pygame
from pygame.sprite import Sprite
class bullet(Sprite):
"一个对飞船发射的子弹进行管理的类"
def __init__(self,ai_settings,screen,ship):
super().__init__()
self.screen=screen
#在(0,0)处创建一个表示子弹的矩形,在设置正确的位置
self.rect=pygame.Rect(0,0,ai_settings.bullet_width,ai_settings.bullet_height)
self.rect.centerx=ship.rect.centerx
self.rect.top=ship.rect.top
#存储用小数表示的子弹位置
self.y=float(self.rect.y)
self.color=ai_settings.bullet_color
self.speed_factor=ai_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)
以上是 最近写的一个飞机大战的程序,写到一半有一个已经写好的函数但不能运用,强行运行程序秒退,百思不得其姐 的全部内容, 来源链接: utcz.com/p/937761.html