你想要直接使用我的这个代码就只需要做一件事,把我这个里面的图片名字给改成你插入的图片名字
这个文件你可以随便取一个一个名字
但是第二个人文件一定要把名字改为wxr.py
#库的调用
import pygame
from random import randint
from wxr import Settings,Ship
import wxr
from pygame.sprite import Group,Sprite
from pygame.sprite import Group
import _thread
#从库里面调用模块并且名字简化
settings = Settings()
# 初始化程序
pygame.init()
screen = pygame.display.set_mode((settings.screen_width,settings.screen_width ))
pygame.display.set_caption("飞机大战")
# 更新显示
pygame.display.flip()
#图片载入--设置背景
image = settings.image
#wall = image
#这里的这个.convert_alpha()实现了透明效果,而pygame.image.load(image).convert
#实现的是覆盖效果并且load会使得速度变快
#image1 = pygame.image.load(image)
#image1 = pygame.image.load(image).convert
background = pygame.image.load(image).convert_alpha()
background = pygame.transform.scale(background, (800, 900))
bullets = Group()
aliens = Group()
ship = Ship(screen,bullets)
wxr.create_fleet(settings, screen, aliens)
#函数不能这么用要不然就会失效
#button = wxr.Button()
#button = button(screen)
button = wxr.Button(screen)
jihuo = wxr.jihuo()
#stats = True
ship.blitme()
#分别是总分数,打怪加分的数字,等级
#打怪加分的数字用来处理总分数score
#这几个参数都是最终要传递到积分(总分数),等级模块里面使用的(等级)
score = 0
jifen = 50
score_dengji = 0
#墙
image1 = pygame.image.load(image)
image1 = pygame.transform.scale(image1, (100, 100))
#提供给weizi1
i = 0
background1 = background
while True:
#起到清除上一次背景图片
screen = pygame.display.set_mode((settings.screen_width, settings.screen_width))
#实现背景交替
weizi = (i, 0)
weizi1 = (i-800,0)
# 界面设置,图像加积分表
screen.blit(background, weizi)
screen.blit(background1, weizi1)
#起到背景参数复位
if i == 800:
i = 0
#墙
#这里的X轴400是对于这个的中间的来说
screen.blit(image1, (400, 450))
#jihuo1_rect = image1.get_rect()
#这是对于头部碰墙
#后面的这个数字越大越往右移动
if ship.rect.centerx > 350 and ship.rect.centerx < 550:
#即使是它的顶部也是需要一个限制的比如说这样的话那么就有可能是限制顶部中间
#这个后面的数字越大越往下
if ship.rect.top < 555 and ship.rect.top > 450:
jihuo.stats_wall = True
ship.move_up = False
#对底部进行限制---还是继续以顶端为参考点
if ship.rect.centerx > 350 and ship.rect.centerx < 550:
#即使是它的顶部也是需要一个限制的比如说这样的话那么就有可能是限制顶部中间
if ship.rect.top > 345 and ship.rect.top < 555:
jihuo.stats_wall = True
ship.move_down = False
#background.subsurface((139, 517, 100, 124))
#把这个东西给加上游戏就会变慢很多
#screen.blit(wall, rect)
#右移
if ship.rect.top < 550 and ship.rect.top > 350:
if ship.rect.centerx > 345 and ship.rect.centerx < 570:
jihuo.stats_wall = True
ship.move_right =False
if ship.rect.top < 550 and ship.rect.top > 350:
if ship.rect.centerx > 350 and ship.rect.centerx < 555:
jihuo.stats_wall = True
ship.move_left = False
wxr.Scoreboard(settings, screen, score).show_score()
wxr.Scoreboard_dengji(settings, screen, score_dengji).show_score()
if jihuo.stats == True:
button.draw_button()
#事件
#函数的这样调用英文调用的那个函数是负责修改另一个函数的参数
#而不是用来处理像stats这样的结果的处理,所以不用return
#函数处理好后可以不用返回
#这样的单独的事件处理没有问题,但是如果两个事件在一起那么就会出现问题老是捕捉的位置不对
#自己飞船的事件处理,处理好了之后会在后面的if中使用(前提是游戏状态激活)
# 状态设置参考点---这里传入状态激活点就是为了设置鼠标点击从而状态初始化
wxr.event(ship,button,jihuo)
# 外星人本来就是自动化的只要下面的在更新那么这里就开始绘制
# 在激活状态下外星人才会添加放到if外面的话那么开始就会显示
aliens.draw(screen)
#这里可以和上面那个if并列使用是因为两个说的都是同一个东西,这个就意味着要么使用if,要么使用else
if jihuo.stats == False:
pygame.mouse.set_visible(False)
if len(aliens) == 0:
#_t