鱼C论坛

 找回密码
 立即注册
查看: 1546|回复: 5

[已解决]求助pygame

[复制链接]
发表于 2022-7-1 09:38:44 | 显示全部楼层 |阅读模式

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

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

x
照着小甲鱼的那个跑图片的程序敲的为啥运行后是黑屏啊
import pygame as pg
import sys

pg.init()

size = width, height = 600, 400
speed = [-2, 1]
bg = (255, 255, 255)

screen = pg.display.set_mode(size)
pg.display.set_caption('hellow world')

sxc = pg.image.load('sxc.jpg')
position = sxc.get_rect()

while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            sys.exit()
            
    position = position.move(speed)

    if position.left < 0 or position.right > width:
        sxc = pg.transform.flip(sxc, True, False)
        speed[0] = -speed[0]

    if position.top < 0 or position.bottom > height:
        speed[1] = -speed[1]

screen.fill(bg)
screen.blit(sxc, position)
pg.display.flip()
pygame.time.delay(10)
最佳答案
2022-7-1 18:15:44
  1. import pygame as pg
  2. import sys

  3. pg.init()

  4. size = width, height = 600, 400
  5. speed = [-2, 1]
  6. bg = (255, 255, 255)

  7. screen = pg.display.set_mode(size)
  8. pg.display.set_caption('hellow world')

  9. sxc = pg.image.load('D:\\turtle.png')
  10. position = sxc.get_rect()

  11. while True:
  12.     for event in pg.event.get():
  13.         if event.type == pg.QUIT:
  14.             sys.exit()
  15.             
  16.     position = position.move(speed)

  17.     if position.left < 0 or position.right > width:
  18.         sxc = pg.transform.flip(sxc, True, False)
  19.         speed[0] = -speed[0]

  20.     if position.top < 0 or position.bottom > height:
  21.         speed[1] = -speed[1]

  22.     # 这里要将下列代码缩进到while循环下,否则下列代码将不会执行
  23.     screen.fill(bg)
  24.     screen.blit(sxc, position)
  25.     pg.display.flip()
  26.     pg.time.delay(10)  # 这里没有pygame,要将pygame改为pg
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-7-1 10:39:25 | 显示全部楼层


255,255,255 就是黑色

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

使用道具 举报

 楼主| 发表于 2022-7-1 11:47:12 | 显示全部楼层
Twilight6 发表于 2022-7-1 10:39
255,255,255 就是黑色

可是改这个数后还是这样
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-1 11:47:52 | 显示全部楼层
d:\1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-1 11:48:22 | 显示全部楼层
d:/1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-1 18:15:44 | 显示全部楼层    本楼为最佳答案   
  1. import pygame as pg
  2. import sys

  3. pg.init()

  4. size = width, height = 600, 400
  5. speed = [-2, 1]
  6. bg = (255, 255, 255)

  7. screen = pg.display.set_mode(size)
  8. pg.display.set_caption('hellow world')

  9. sxc = pg.image.load('D:\\turtle.png')
  10. position = sxc.get_rect()

  11. while True:
  12.     for event in pg.event.get():
  13.         if event.type == pg.QUIT:
  14.             sys.exit()
  15.             
  16.     position = position.move(speed)

  17.     if position.left < 0 or position.right > width:
  18.         sxc = pg.transform.flip(sxc, True, False)
  19.         speed[0] = -speed[0]

  20.     if position.top < 0 or position.bottom > height:
  21.         speed[1] = -speed[1]

  22.     # 这里要将下列代码缩进到while循环下,否则下列代码将不会执行
  23.     screen.fill(bg)
  24.     screen.blit(sxc, position)
  25.     pg.display.flip()
  26.     pg.time.delay(10)  # 这里没有pygame,要将pygame改为pg
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 04:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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