斌小白 发表于 2020-11-28 17:29:01

pygame第一讲

import pygame
import sys

pygame.init()
#初始化pygame

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

screen = pygame.display.set_mode(size)
#创建指定大小窗口
pygame.display.set_caption('初次见面,请多多关照!')
#设置窗口标题

turtle = pygame.image.load('turtle.png')
#加载图片
position = turtle.get_rect()
#获得图像的位置矩形

while True :
    for event in pygame.event.get() :
      if event.type == pygame.QUIT :
            sys.exit()

    position = position.move(speed)
    #移动图像

    if position.left < 0 or position.right > width :
      turtle = pygame.transform.flip(turtle, True, False)
      #翻转图像
      speed = -speed
      #反方向移动

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

    screen.fill(bg)
    #填充背景
    screen.blit(turtle, position)
    #更新图像
    pygame.display.flip()
    #更新界面
    pygame.time.delay(20)
    #延迟20秒
pygame第一讲遇到的问题,一直显示FileNotFoundError: No such file or directory.请问图片那块该咋设置呢?

Twilight6 发表于 2020-11-28 17:46:24



将图片保存在你当前运行脚本文件的目录下,命名为turtle.png

或者找到你图片文件绝对路径,拷贝路径将 pygame.image.load('文件绝对路径') 里面的文件绝对路径更换为你刚刚拷贝的路径即可

斌小白 发表于 2020-11-28 19:25:30

谢谢{:5_109:}
页: [1]
查看完整版本: pygame第一讲