|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import pygame
from pygame.locals import *
from sys import exit
background_image_filename = 'sea.jpg'
mouse_image_filename = 'fish.png'
# 初始化pygame,为使用硬件做准备
pygame.init()
# 创建一个窗口
screen = pygame.display.set_mode((640, 480), 0, 32)
# 设置窗口标题
pygame.display.set_caption("hello,world!")
# 加载图片并转换
background = pygame.image.load(background_image_filename)
mouse_cursor = pygame.image.load(mouse_image_filename)
# 游戏主循环
while True:
for event in pygame.event.get():
if event.type == QUIT:
# 接收到退出时间后退出程序
exit()
# 将背景图画上去
screen.blit(background, (0, 0))
# 获得鼠标位置
x, y = pygame.mouse.get_pos()
# 计算光标左上角位置
x -= mouse_cursor.get_width() / 2
y -= mouse_cursor.get_height() / 2
# 将光标画上去
screen.blit(mouse_cursor, (x, y))
# 刷新画面
pygame.display.update()
这个运行之后是黑屏 我之前好像运行过可以呀 但是现在弄 怎么弄都是黑屏 不知道问题出现在哪里 那两个图片文件sea.jpg 和 fish.png 默认是不是放在python安装的目录下 也就是.py的默认储存位置?
最后一行缩进有问题,改成这样就好了:
- import pygame
- from pygame.locals import *
- from sys import exit
- background_image_filename = 'sea.jpg'
- mouse_image_filename = 'fish.png'
- # 初始化pygame,为使用硬件做准备
- pygame.init()
- # 创建一个窗口
- screen = pygame.display.set_mode((640, 480), 0, 32)
- # 设置窗口标题
- pygame.display.set_caption("hello,world!")
- # 加载图片并转换
- background = pygame.image.load(background_image_filename)
- mouse_cursor = pygame.image.load(mouse_image_filename)
- # 游戏主循环
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- # 接收到退出时间后退出程序
- exit()
- # 将背景图画上去
- screen.blit(background, (0, 0))
- # 获得鼠标位置
- x, y = pygame.mouse.get_pos()
- # 计算光标左上角位置
- x -= mouse_cursor.get_width() / 2
- y -= mouse_cursor.get_height() / 2
- # 将光标画上去
- screen.blit(mouse_cursor, (x, y))
- # 刷新画面
- pygame.display.update()
复制代码
|
|