pygame-飞机大战1.0(步骤+窗口无响应问题)

一、安装pygame

详见pygame安装-Requirement already satisfied问题_Kiraxqc的博客-CSDN博客

二、熟悉pygame命令操作

import pygame # 导入模块
pygame.init()  # 初始化
# 游戏代码编写
print("游戏代码")
pygame.quit()  # 卸载所有pygame模块,game over的时候

 1. 坐标系

背景左上角为(0,0),游戏为多个静止图片组成,需要确定具体坐标系位置

pygame提供一个类pygame.Rect用于描述矩形区域

Rect (x, y, width, height):  (x轴位置,y轴位置,图片宽度,图片高度)

import pygame
hero_rect = pygame.Rect(100, 500, 120, 125)
print("原点 %d %d" % (hero_rect.x, hero_rect.y))
print("尺寸 %d %d" % (hero_rect.width, hero_rect.height))
print("%d %d" % hero_rect.size) # size属性返回元组(width, height)

2. 创建游戏窗口

使用python.display.set_mode()    # 使用pygame.display模块,调用set_code方法

Set_mode(resolution=(0,0), flags=0, depth=0)

resolution:屏幕宽高,默认与屏幕大小一致

flags:窗口附加选项,默认不传递

depth:颜色位置,默认自动匹配

import pygame
pygame.init()
#创建游戏  display模块的set_mode方法  480*700
pygame.display.set_mode((480, 700)) # ()不填数据则会创建与屏幕相同大的窗口
while True:  # 游戏循环
    pass
pygame.quit()

3. 游戏背景图片导入

第一步:加载到内存 pygame.image.load()

第二步:使用游戏屏幕对象,blit方法,将图片绘制到指定位置

第三步:调用pygame.display.update()方法屏幕显示

import pygame
pygame.init()
#创建游戏  display模块的set_mode方法  480*700
screen = pygame.display.set_mode((480, 700)) # ()不填数据则会创建与屏幕相同大的窗口
# 创建背景图像
# 1)加载背景图像
bg = pygame.image.load("./images/background.png")  #注意图片位置
# blit 绘制图像
screen.blit(bg, (0,0))  # (0,0)为图片左上角坐标
# 更新图像
pygame.display.update()

while True:  # 游戏循环
    pass
pygame.quit()

出错分析: blit拼写错误;pygame.display.set_mode为设置返回值

运行结果:

1)打开时会有一秒黑屏

2)窗口显示未响应

请问有朋友知道该怎么解决吗?谢谢大家


根据网上的操作添加了如下代码,可以运行成功,但是退出时不能点击“x”,必须点击pycharm中的停止键

while True:
    pygame.event.get() #从事件队列中获取一个事件,并从队列中删除该事件
pygame.quit()

但是不知道原因.... 

猜你喜欢

转载自blog.csdn.net/Kiraxqc/article/details/125778200