当前位置:网站首页>pygame-飞机大战1.0(步骤+窗口无响应问题)
pygame-飞机大战1.0(步骤+窗口无响应问题)
2022-07-17 05:03:00 【Kiraxqc】
一、安装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()但是不知道原因....
边栏推荐
猜你喜欢
![Service end interface test - test point of interface test [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]](/img/62/ae0d6f5654c1352437ac99947d25e4.png)
Service end interface test - test point of interface test [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]

Conception finale: système distribué de gestion de la santé pour la prévention des épidémies hautement simultanées basé sur vue + socket + redis

DSL search results processing, including sorting, paging, highlighting

FanoutExchange交换机简单使用

DSL搜索结果处理,包括排序,分页,高亮

Basic operations of index library operation

浅聊全局过滤器

MySQL fuzzy matching 1, 11111 similar string problems

一文了解Zipkin

sleuth入门
随机推荐
TiDB 性能优化概述
接口的参数返回封装类Result
Learn about scheduled tasks in one article
Emqx pressure test tread pit for your reference
毕设:基于Vue+Socket+Redis的分布式高并发防疫健康管理系统
DirectExchange交换机的简单使用。
MySQL takes the union of two query conditions and then queries
One article to understand Zipkin
图片上传的逻辑
POC——DVWA‘s SQL Injection
一文带你了解HAProxy
Using circular statements to make login programs
Chat about global filter
Conception finale: système distribué de gestion de la santé pour la prévention des épidémies hautement simultanées basé sur vue + socket + redis
ThinkPHP official website tutorial
浅聊全局过滤器
redis 安装
索引库操作基本操作
Cve-2020-10199 recurrence of nexus repository manager3 remote command execution vulnerability
Bank link No. cnasp & Query (II)