Pygame 外星人入侵(一)武装飞船

1、初始化

    * Pygame初始化

pygame.init()

2、屏幕初始化(display)

     * 初始化窗口

screen = pygame.display.set_mode((1200,800)

     实际上pygame.display.set_mode()这个函数会返回一个Surface对象,他是位图的一种。   

    * 设置标题

pygame.display.set_caption("Alien Invasion")

    * 设置背景颜色

bg_color = (230,230,230)
screen.fill(bg_color)

    * 让最近绘制的屏幕可见

pygame.display.flip()     

    * 设置背景图片

background=pygame.image.load("timg.jpg").convert_alpha()  
screen.blit(background,(0,0)) 

    * 更新显示屏幕

pygame.display.update()
    update()更新部分软件界面显示
    flip()更新整个待显示的Surface对象到屏幕上

    * 加载和显示图标

#加载图标  
icon=pygame.image.load("timg.jpg").convert_alpha()  
#显示图标  
pygame.display.set_icon(icon)  

3、加载和显示图像

    * 加载图像

image = pygame.image.load('images/ship.bmp')

    * 获取位图的宽和高

width,height = image.size()

rect = image.get_rect()
screen_rect = screen.get_rect()


rect.centerx = screen_rect.centerx
rect.bottom = screen_rect.bottom


4、 监视鼠标和键盘事件

for event in pygame.event.get():
    if event.type==pygame.QUIT:
        sys.exit()


5、 在屏幕指定位置绘制图像

screen.blit(image,rect)

    例:在屏幕中心绘制图像

planet = pygame.image.load("earth.png").convert_alpha()
#获取位图的宽和高
width,height = planet.get_size()
#在屏幕的中间绘制地球
screen.blit(planet, (400-width/2,300-height/2))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值