鱼C论坛

 找回密码
 立即注册
查看: 1019|回复: 2

[已解决]求教 请大神指教

[复制链接]
发表于 2019-10-5 14:36:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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的默认储存位置?
最佳答案
2019-10-5 14:37:28
最后一行缩进有问题,改成这样就好了:
  1. import pygame

  2. from pygame.locals import *
  3. from sys import exit
  4. background_image_filename = 'sea.jpg'
  5. mouse_image_filename = 'fish.png'

  6. # 初始化pygame,为使用硬件做准备
  7. pygame.init()

  8. # 创建一个窗口
  9. screen = pygame.display.set_mode((640, 480), 0, 32)

  10. # 设置窗口标题
  11. pygame.display.set_caption("hello,world!")

  12. # 加载图片并转换
  13. background = pygame.image.load(background_image_filename)
  14. mouse_cursor = pygame.image.load(mouse_image_filename)

  15. # 游戏主循环
  16. while True:
  17.     for event in pygame.event.get():
  18.         if event.type == QUIT:
  19.             # 接收到退出时间后退出程序
  20.             exit()

  21.     # 将背景图画上去
  22.     screen.blit(background, (0, 0))

  23.     # 获得鼠标位置
  24.     x, y = pygame.mouse.get_pos()
  25.     # 计算光标左上角位置
  26.     x -= mouse_cursor.get_width() / 2
  27.     y -= mouse_cursor.get_height() / 2

  28.     # 将光标画上去
  29.     screen.blit(mouse_cursor, (x, y))

  30.     # 刷新画面
  31.     pygame.display.update()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-10-5 14:37:28 | 显示全部楼层    本楼为最佳答案   
最后一行缩进有问题,改成这样就好了:
  1. import pygame

  2. from pygame.locals import *
  3. from sys import exit
  4. background_image_filename = 'sea.jpg'
  5. mouse_image_filename = 'fish.png'

  6. # 初始化pygame,为使用硬件做准备
  7. pygame.init()

  8. # 创建一个窗口
  9. screen = pygame.display.set_mode((640, 480), 0, 32)

  10. # 设置窗口标题
  11. pygame.display.set_caption("hello,world!")

  12. # 加载图片并转换
  13. background = pygame.image.load(background_image_filename)
  14. mouse_cursor = pygame.image.load(mouse_image_filename)

  15. # 游戏主循环
  16. while True:
  17.     for event in pygame.event.get():
  18.         if event.type == QUIT:
  19.             # 接收到退出时间后退出程序
  20.             exit()

  21.     # 将背景图画上去
  22.     screen.blit(background, (0, 0))

  23.     # 获得鼠标位置
  24.     x, y = pygame.mouse.get_pos()
  25.     # 计算光标左上角位置
  26.     x -= mouse_cursor.get_width() / 2
  27.     y -= mouse_cursor.get_height() / 2

  28.     # 将光标画上去
  29.     screen.blit(mouse_cursor, (x, y))

  30.     # 刷新画面
  31.     pygame.display.update()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-5 14:42:53 | 显示全部楼层
zltzlt 发表于 2019-10-5 14:37
最后一行缩进有问题,改成这样就好了:

厉害来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-21 18:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表