鱼C论坛

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

零基础入门python第81讲

[复制链接]
发表于 2019-7-3 20:46:48 | 显示全部楼层 |阅读模式

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

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

x
  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. # 初始化Pygame
  5. pygame.init()

  6. size = width, height = 600, 400
  7. speed = [-2, 1]
  8. bg = (255, 255, 255) # RGB

  9. # 创建指定大小的窗口 Surface
  10. screen = pygame.display.set_mode(size)
  11. # 设置窗口标题
  12. pygame.display.set_caption("初次见面,请大家多多关照!")

  13. # 加在图片
  14. turtle = pygame.image.load("turtle.png")
  15. # 获得图像的位置矩形
  16. position = turtle.get_rect()

  17. l_head = turtle.get_rect()
  18. r_head = pygame.transform.flip(turtle, True, False)

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

  23.         if event.type ==KEYDOWN:
  24.             if event.key ==K_LEFT:
  25.                 turtle = l_head
  26.                 speed = [-1,0]
  27.             if event.key ==K_RIGHT:
  28.                 turtle = r_head
  29.                 speed = [1,0]
  30.             if event.key ==K_UP:
  31.                 speed = [0,-1]
  32.             if event.key ==K_DOWN:
  33.                 speed = [0,1]

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

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

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

  43.     # 填充背景
  44.     screen.fill(bg)
  45.     # 更新图像
  46.     screen.blit(turtle, position)
  47.     # 更新界面
  48.     pygame.display.flip()
  49.     # 延迟10毫秒
  50.     pygame.time.delay(10)
复制代码
为什么会报错?
QQ截图20190703204615.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-7-4 00:17:20 | 显示全部楼层
没有学过这个游戏库,但是根据报错的意思来看是57行的screen.blit()这个函数的第一个参数类型不对啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-4 02:12:38 | 显示全部楼层
牛逼,80节了,我才撸到第30节,速度太慢了。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-16 16:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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