[关闭]
@websec007 2020-05-31T22:22:17.000000Z 字数 21705 阅读 1376

pygame 游戏开发入门 - 北理工公开课

pygame


第二单元

2.1 pygame 简介与安装

  1. # pip install pygame --index-url https://mirrors.aliyun.com/pypi/simple/
  2. # pip install pygame -i https://mirrors.aliyun.com/pypi/simple/
  1. # python -m pygame.examples.alien

2.2 pygame游戏开发最小代码框架

  1. import pygame, sys
  2. # 1. pygame模块初始化
  3. pygame.init()
  4. # 2. 屏幕设置
  5. size = width, height = 360, 480
  6. screen = pygame.display.set_mode(size)
  7. pygame.display.set_caption("pygame最小代码框架测试")
  8. bg_color = (233, 233, 233)
  9. # 3. 游戏主循环
  10. while True:
  11. """(1) Input Process"""
  12. for event in pygame.event.get():
  13. if event.type == pygame.QUIT:
  14. sys.exit()
  15. """(2) Game Update"""
  16. screen.fill(bg_color)
  17. """(3) Render"""
  18. pygame.display.update()

2.3 壁球小游戏之图片外接矩形的基本使用(展示型)

  1. import pygame, sys
  2. # 1. pygame模块初始化
  3. pygame.init()
  4. # 2. 屏幕设置
  5. size = width, height = 360, 480
  6. screen = pygame.display.set_mode(size)
  7. pygame.display.set_caption("pygame最小代码框架测试")
  8. bg_color = (233, 233, 233)
  9. ''' 弹力球 image 载入'''
  10. ball_image= pygame.image.load(r"F:\My_Learn\PygameCourses\images\PYG02-ball.gif")
  11. ball_rect = ball_image.rect_get()
  12. speed = [1, 1]
  13. # 3. 游戏主循环
  14. while True:
  15. """(1) Input Process"""
  16. for event in pygame.event.get():
  17. if event.type == pygame.QUIT:
  18. sys.exit()
  19. """(2) Game Update"""
  20. '''设置弹力球反弹的边界范围'''
  21. ball_rect = ball_rect.move(speed)
  22. if ball_rect.left < 0 or ball_rect.right > width:
  23. speed[0] = - speed[0]
  24. if ball_rect.top < 0 or ball_rect.bottom > height:
  25. speed[1] = - speed[1]
  26. """(3) Render"""
  27. screen.fill(bg_color)
  28. screen.blit(ball_image, ball_rect)
  29. pygame.display.update()

2.4 壁球小游戏之屏幕刷新基本设置(节奏型)

  1. import pygame, sys
  2. # 1. pygame模块初始化
  3. pygame.init()
  4. # 2. 屏幕设置
  5. size = width, height = 720, 420
  6. screen = pygame.display.set_mode(size)
  7. pygame.display.set_caption("pygame 最小代码框架测试")
  8. bg_color = (233, 233, 233)
  9. ball_image = pygame.image.load(r"F:\My_Learn\PygameCourses\images\PYG02-ball.gif")
  10. ball_rect = ball_image.get_rect()
  11. speed = [1, 1]
  12. """创建clock对象,为设置屏幕刷新做准备"""
  13. fps = 300
  14. clock = pygame.time.Clock()
  15. # 3. 游戏主循环
  16. while True:
  17. """(1) Input Process"""
  18. for event in pygame.event.get():
  19. if event.type == pygame.QUIT:
  20. sys.exit()
  21. """(2) Game Update"""
  22. ball_rect = ball_rect.move(speed)
  23. if ball_rect.left < 0 or ball_rect.right > width:
  24. speed[0] = - speed[0]
  25. if ball_rect.top < 0 or ball_rect.bottom > height:
  26. speed[1] = - speed[1]
  27. """(3) Render"""
  28. clock.tick(fps)
  29. screen.fill(bg_color)
  30. screen.blit(ball_image, ball_rect)
  31. pygame.display.update()

2.5 壁球小游戏之键盘操作事件类型(操控型)

  1. import pygame, sys
  2. # 1. pygame模块初始化
  3. pygame.init()
  4. # 2. 屏幕设置
  5. size = width, height = 720, 420
  6. screen = pygame.display.set_mode(size)
  7. pygame.display.set_caption("pygame 最小代码框架测试")
  8. bg_color = (233, 233, 233)
  9. ball_image = pygame.image.load(r"F:\My_Learn\PygameCourses\images\PYG02-ball.gif")
  10. ball_rect = ball_image.get_rect()
  11. speed = [1, 1]
  12. """创建clock对象,为设置屏幕刷新做准备"""
  13. fps = 200
  14. clock = pygame.time.Clock()
  15. # 3. 游戏主循环
  16. while True:
  17. """(1) Input Process"""
  18. for event in pygame.event.get():
  19. if event.type == pygame.QUIT:
  20. sys.exit()
  21. """通过键盘类型-键盘值的调整实现对speed值的横坐标与纵坐标进行重新赋值"""
  22. elif event.type == pygame.KEYDOWN:
  23. if event.key == pygame.K_LEFT:
  24. speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0] - 1)*int(speed[0]/abs(speed[0])))
  25. elif event.key == pygame.K_RIGHT:
  26. speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
  27. elif event.key == pygame.K_UP:
  28. speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
  29. elif event.key == pygame.K_DOWN:
  30. speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1] - 1)*int(speed[1]/abs(speed[1])))
  31. """(2) Game Update"""
  32. print(speed)
  33. ball_rect = ball_rect.move(speed)
  34. if ball_rect.left < 0 or ball_rect.right > width:
  35. speed[0] = - speed[0]
  36. if ball_rect.top < 0 or ball_rect.bottom > height:
  37. speed[1] = - speed[1]
  38. """(3) Render"""
  39. clock.tick(fps)
  40. screen.fill(bg_color)
  41. screen.blit(ball_image, ball_rect)
  42. pygame.display.update()

第三单元

3.1 单元开篇

3.2 屏幕绘制机制简介

3.3 屏幕“尺寸”与“模式”设置及“信息获取”

3.3.1 屏幕“尺寸”和“模式”设置函数

3.3.2 屏幕显示模式单独使用问题

3.3.3 源码演示 - 屏幕显示模式

  1. # 2. 屏幕模式设置
  2. size = width, height = 720, 420
  3. print(pygame.display.Info())
  4. # screen = pygame.display.set_mode(size, pygame.RESIZABLE)
  5. # screen = pygame.display.set_mode(size, pygame.NOFRAME)
  6. # screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
  7. screen = pygame.display.set_mode(size)
  8. print(pygame.display.Info())
  9. pygame.display.set_caption("pygame 最小代码框架测试")
  10. bg_color = (233, 233, 233)
  11. ball_image = pygame.image.load(r"F:\My_Learn\PygameCourses\images\PYG02-ball.gif")
  12. ball_rect = ball_image.get_rect()
  13. speed = [1, 1]

3.4 屏幕信息函数

3.4.1 屏幕尺寸信息获取

  1. vInfo = pygame.display.Info()
  2. size = width, height = vInfo.current_w, vInfo.current_h
  1. 1current_w: 当前窗口像素的宽度
  2. 2current_h: 当前窗口像素的高度

-----------------------------以下为运行的源码部分--------------------------------

  1. import pygame, sys
  2. # 1. pygame模块初始化
  3. pygame.init()
  4. # 2. 屏幕设置
  5. size = width, height = 360, 480
  6. """屏幕大小感知:pygame.display.Info()"""
  7. print(pygame.display.Info())
  8. screen = pygame.display.set_mode(size)
  9. print(pygame.display.Info())
  10. pygame.display.set_caption("pygame最小代码框架测试")
  11. bg_color = (233, 233, 233)
  12. # 3. 游戏主循环
  13. while True:
  14. ...

-----------------------------以下为源码运行打印输出部分--------------------------------

  1. # 屏幕分辨率尺寸设置前:
  2. <VideoInfo(hw = 0, wm = 1,video_mem = 0
  3. blit_hw = 0, blit_hw_CC = 0, blit_hw_A = 0,
  4. blit_sw = 0, blit_sw_CC = 0, blit_sw_A = 0,
  5. bitsize = 32, bytesize = 4,
  6. masks = (16711680, 65280, 255, 0),
  7. shifts = (16, 8, 0, 0),
  8. losses = (0, 0, 0, 8),
  9. current_w = 1920, current_h = 1080
  10. # 屏幕分辨率尺寸设置后:
  11. <VideoInfo(hw = 0, wm = 1,video_mem = 0
  12. blit_hw = 0, blit_hw_CC = 0, blit_hw_A = 0,
  13. blit_sw = 0, blit_sw_CC = 0, blit_sw_A = 0,
  14. bitsize = 32, bytesize = 4,
  15. masks = (16711680, 65280, 255, 0),
  16. shifts = (16, 8, 0, 0),
  17. losses = (0, 0, 0, 8),
  18. current_w = 360, current_h = 480
通过源码运行演示打印,我们可以看到在 pygame.display.set_mode()前后,当前屏幕显示信息的变化;

3.4.2 屏幕尺寸动态调整

  1. # 3. 游戏主循环
  2. while True:
  3. """(1) Input Process"""
  4. for event in pygame.event.get():
  5. if event.type == pygame.QUIT:
  6. sys.exit()
  7. elif event.type == pygame.KEYDOWN:
  8. if event.key == pygame.K_LEFT:
  9. speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0] - 1)*int(speed[0]/abs(speed[0])))
  10. elif event.key == pygame.K_RIGHT:
  11. speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
  12. elif event.key == pygame.K_UP:
  13. speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
  14. elif event.key == pygame.K_DOWN:
  15. speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1] - 1)*int(speed[1]/abs(speed[1])))
  16. elif event.key == pygame.K_ESCAPE:
  17. sys.exit()
  18. """屏幕大小动态调整感知和自动调整实现"""
  19. elif event.type == pygame.VIDEORESIZE:
  20. size = width, height = event.size[0], event.size[1]
  21. screen = pygame.display.set_mode(size, pygame.RESIZABLE)
  22. """(2) Game Update"""

3.5 屏幕标题和图标控制

3.5.1 屏幕标题设置

3.5.2 屏幕标题获取

3.5.3 屏幕图标控制

  1. icon = pygame.image.load('icon.png')
  2. pygame.display.set_icon(icon)

3.6 屏幕最小化感知和刷新运用

3.6.1 窗口最小化感知

  1. .....
  2. if pygame.display.get_active():
  3. ball_rect = ball_rect.move(speed)
  4. if ball_rect.left < 0 or ball_rect.right > width:
  5. speed[0] = - speed[0]
  6. if ball_rect.top < 0 or ball_rect.bottom > height:
  7. speed[1] = - speed[1]

3.6.2 屏幕刷新函数


第四单元

4.1 单元开篇


4.2 Pygame 事件处理机制简介

注:事件处置机制的核心内容就是事件队列的缓存,又称“事件队列”。

4.2.1 事件处理需求

4.2.2 事件处理类型

4.2.3 pygame 事件类型以及属性

序号 事件 事件类型 事件属性
1 系统 pygame.QUIT
pygame.ACTIVEEVENT
none
gain, state
2 键盘 pygame.KEYDOWN
pygame.KEYUP
event.unicode event.key event.mod
3 鼠标 pygame.MOUSEMOTION
pygame.MOUSEBOTTONDOWN
pygame.MOUSEBOTTONUP
pos,rel,button
pos,button
pos,button
4 窗口 pygame.VIDEORESIZE
pygame.VIDEOEXPOSE
size,w,h
none
5 游戏杆 JOY ... joy ...
6 自定义 USEREVENT code

4.3 键盘事件以及属性(2类)

4.3.1 键盘事件及其属性

4.3.2 按键常量与修饰符

4.3.3 按键示例演示

  1. # Description: pygame.KEYDWON 键盘按键事件响应测试
  2. import pygame, sys
  3. ...
  4. # 3. 游戏主循环
  5. while True:
  6. """(1) Input Process"""
  7. for event in pygame.event.get():
  8. if event.type == pygame.QUIT:
  9. sys.exit()
  10. # 对键盘按下事件进行测试学习:了解其3个属性,特别是event.unicode
  11. elif event.type == pygame.KEYDOWN:
  12. if event.unicode == '':
  13. print("[KEYDOWN]: {}, {}, {}".format("#", event.key, event.mod))
  14. else:
  15. print("[KEYUP]: {}, {}, {}".format(event.unicode, event.key, event.mod))
  16. .....
  17. """(3) Render"""
  18. pygame.display.update()

4.4 鼠标事件以及其属性(3类)

4.4.1 鼠标事件与其属性

4.4.2 鼠标事件演示

  1. # 3. 游戏主循环
  2. while True:
  3. """(1) Input Process"""
  4. for event in pygame.event.get():
  5. if event.type == pygame.QUIT:
  6. sys.exit()
  7. # 对键盘按下事件进行测试学习:了解其3个属性,特别是event.unicode
  8. elif event.type == pygame.KEYDOWN:
  9. if event.unicode == '':
  10. print("[KEYDOWN]: {}, {}, {}".format("#", event.key, event.mod))
  11. else:
  12. print("[KEYDOWN]: {}, {}, {}".format(event.unicode, event.key, event.mod))
  13. # 对鼠标3事件和其属性进行测试与学习
  14. elif event.type == pygame.MOUSEMOTION:
  15. print("[MOUSEMOTON]: {}, {}, {}".format(event.pos, event.rel, event.buttons))
  16. elif event.type == pygame.MOUSEBUTTONDOWN:
  17. print("[MOUSEBUTTONDOWN]: {}, {}".format(event.pos, event.button))
  18. elif event.type == pygame.MOUSEBUTTONUP:
  19. print("[MOUSEBUTTONUP]: {}, {}".format(event.pos, event.button))

4.5 壁球小游戏(鼠标型)

4.5.1 游戏需求

(1)通过鼠标左键摆放壁球,按下按键时壁球停止移动;
(2)按键按下并且移动时,壁球跟随鼠标移动;
(3)当释放时,壁球继续移动;

4.5.2 游戏源码

  1. # 3. 游戏主循环
  2. while True:
  3. """(1) Input Process"""
  4. .....
  5. # 增加对鼠标控制的响应
  6. # (1) 鼠标左键按下,壁球暂停移动(通过 still = True 配合默认移动条件实现)
  7. elif event.type == pygame.MOUSEBUTTONDOWN:
  8. if event.button == 1:
  9. still = True
  10. # (2) 鼠标按键释放,壁球移动到鼠标当前位置;
  11. elif event.type == pygame.MOUSEBUTTONUP:
  12. if event.button == 1:
  13. still = False
  14. ball_rect = ball_rect.move(event.pos[0] - ball_rect.left, event.pos[1] - ball_rect.top)
  15. # (3) 鼠标移动,壁球跟着移动
  16. elif event.type == pygame.MOUSEMOTION:
  17. if event.buttons[0] == 1:
  18. ball_rect = ball_rect.move(event.pos[0] - ball_rect.left, event.pos[1] - ball_rect.top)
  19. """(2) Game Update"""
  20. # 屏幕窗口最小化感知判断,从而决定是否要暂停壁球的移动
  21. if pygame.display.get_active() and not still:
  22. ball_rect = ball_rect.move(speed[0], speed[1])
  23. # 壁球的边界控制
  24. if ball_rect.left < 0 or ball_rect.right > width:
  25. speed[0] = - speed[0]
  26. if width < ball_rect.right < ball_rect.right + speed[0]:
  27. speed[0] = - speed[0]
  28. if ball_rect.top < 0 or ball_rect.bottom > height:
  29. speed[1] = - speed[1]
  30. if height < ball_rect.bottom < ball_rect.bottom + speed[1]:
  31. speed[1] = - speed[1]
  32. """(3) Render"""

4.6 pygame事件处理函数

4.6.1 处理事件函数

  1. for event in pygame.event.get():
  2. if event.type == pygame.QUIT:
  3. sys.exit()

4.6.2 操作事件队列函数

4.6.3 生成事件函数


第五单元

5.1 单元开篇


5.2 pygame 色彩绘制机制

5.2.1 色彩机制 pygame.Color

5.2.2 色彩版小游戏

  1. # 色彩控制
  2. bg_color = pygame.Color("grey")
  3. def RGBChannel(a):
  4. return 0 if a<0 else (255 if a>255 else int(a))
  5. # 3. 游戏主循环
  6. while True:
  7. ....
  8. # 通过pygame.Color.r来绘制色彩
  9. bg_color.r = RGBChannel(ball_rect.left/width * 255)
  10. bg_color.g = RGBChannel(ball_rect.top/height * 255)
  11. bg_color.b = RGBChannel(min(speed)/ max(speed[0], speed[1], 1) * 255)
  12. bg_color.a = RGBChannel(ball_rect.left/width * 255)
  13. screen.fill(bg_color)

5.3 pygame 多图形绘制

5.3.1 pygam.draw 绘图简介

pygame.draw.xxx()方法 进行图形绘制过程,其就是在屏幕画布上直接进行图形绘制,然后返回一个pygame.Rect类表示该形状;

5.3.2 pygame.Rect 外接矩形对象

5.3.3 pygame.draw 绘图方法

5.3.4 图形绘制源码实例

  1. import pygame,sys
  2. from math import pi
  3. # 1.初始化
  4. pygame.init()
  5. # 2. 屏幕初始化
  6. size = width, height = 720, 600
  7. # Vinfo = pygame.display.Info()
  8. # size = width, height = Vinfo.current_w, Vinfo.current_h
  9. screen = pygame.display.set_mode(size)
  10. pygame.display.set_caption("多图形绘制学习")
  11. bg_color = pygame.Color('black')
  12. GOLD = 255, 251, 0
  13. RED = pygame.Color('red')
  14. WHITE = 255, 255, 255
  15. GREEN = pygame.Color('green')
  16. # # 多图绘制
  17. # e1rect = pygame.draw.ellipse(screen, GREEN, (50, 50, 500, 500), 3)
  18. # c1rect = pygame.draw.circle(screen, GOLD, (200, 180), 30, 5)
  19. # c2rect = pygame.draw.circle(screen, GOLD, (400, 180), 30)
  20. # r1rect = pygame.draw.rect(screen, RED, (170, 130, 60, 10), 3)
  21. # r2rect = pygame.draw.rect(screen, RED, (370, 130, 60, 10))
  22. # plist = [(295,170), (285,250), (260,280), (340,280), (315,250), (305 ,170)]
  23. # l1rect = pygame.draw.lines(screen, GOLD, True, plist, 2)
  24. # a1rect = pygame.draw.arc(screen, RED, (200, 220, 200, 100), 1.4*pi, 1.9*pi, 3)
  25. # 3. game loop
  26. while True:
  27. # process input
  28. for event in pygame.event.get():
  29. if event.type == pygame.QUIT:
  30. sys.exit()
  31. elif event.type == pygame.KEYDOWN:
  32. if event.key == pygame.K_ESCAPE:
  33. pygame.quit()
  34. elif event.type == pygame.VIDEORESIZE:
  35. size = width, height = event.size[0], event.size[1]
  36. screen = pygame.display.set_mode(size, pygame.RESIZABLE)
  37. # update game
  38. screen.fill(bg_color)
  39. # 多图绘制
  40. e1rect = pygame.draw.ellipse(screen, GREEN, (50, 50, 500, 500), 3)
  41. c1rect = pygame.draw.circle(screen, GOLD, (200, 180), 30, 5)
  42. c2rect = pygame.draw.circle(screen, GOLD, (400, 180), 30)
  43. r1rect = pygame.draw.rect(screen, RED, (170, 130, 60, 10), 3)
  44. r2rect = pygame.draw.rect(screen, RED, (370, 130, 60, 10))
  45. plist = [(295, 170), (285, 250), (260, 280), (340, 280), (315, 250), (305, 170)]
  46. l1rect = pygame.draw.lines(screen, GOLD, True, plist, 2)
  47. a1rect = pygame.draw.arc(screen, RED, (200, 220, 200, 100), 1.4 * pi, 1.9 * pi, 3)
  48. # render
  49. pygame.display.update()

5.3.5 学习心得

图形绘制过程:是直接在指定屏幕画布上进行作画的,图形会直接在屏幕画布中显示;(图形绘制过程与“图片”与“文字”绘制是不同的;

图形与文字的绘制过程是,先是由外部导入的一个对象并获得其矩形对象属性然后通过surface.blit()方法绘制到屏幕画布上

5.4 文字绘制

5.4.1 文字绘制基础知识

5.4.2 文字绘制详解

5.4.3 源码演示

方法1:

  1. # 文字绘制 - 方法1
  2. Font = pygame.font.Font(r'F:\My_Learn\PygameCourses\fonts\msyh.ttc', 36)
  3. text_surface = Font.render('不要急,慢一点,少一点', True, GOLD)
  4. # print(text_surface)
  5. text_rect = text_surface.get_rect()
  6. # print(text_rect)
  7. # 文字图层移入主图层,显示出来
  8. screen.blit(text_surface, text_rect)

方法2:

  1. # 文字绘制 - 方法2
  2. # 导入模块
  3. import pygame.freestyle
  4. # 字符绘制,并获取Rect对象;
  5. Font = pygame.freetype.Font(r'F:\My_Learn\PygameCourses\fonts\msyh.ttc', 36)
  6. text_surface, text_rect = Font.render('世界和平', fgcolor=pygame.Color('gold'), size=50)
  7. print(text_rect)
  8. # 文字图层移入主图层,显示出来
  9. screen.blit(text_surface, text_rect)

5.5


课程学习小结

序号 函数 功能说明
1 pygame.display.Info() 屏幕信息获取
2 pygame.display.set_mode(size, flags) 屏幕尺寸与模式设置
3 pygame.display.set_icon(icon_surface) 屏幕图标设置
4 pygame.display.set_caption('Title_Strings') 设置屏幕标题
5 pygame.display.get_caption() 屏幕标题信息获取
6 pygame.display.get_active() 屏幕最小化感知判断
7 pygame.display.flip() 屏幕整个刷新
8 pygame.display.update() 仅刷新更新的内容
序号 事件 事件类型 事件属性
1 系统事件 pygame.QUIT
pygame.ACTIVEEVENT
none
gain, state
2 键盘事件 pygame.KEYDOWN
pygame.KEYUP
event.unicode event.key event.mod
event.key event.mod
3 鼠标事件 pygame.MOUSEMOTION
pygame.MOUSEBOTTONDOWN
pygame.MOUSEBOTTONUP
pos,rel,button
pos,button
pos,button
4 窗口事件 pygame.VIDEORESIZE
pygame.VIDEOEXPOSE
size,w,h
none
5 游戏杆事件 JOY ... joy ...
6 自定义事件 USEREVENT code

宝贵的错误集

pygame.Rect 对象

  1. # 多图形绘制 (图形绘制与显示是1步完成的,无需在blit())
  2. e1rect = pygame.draw.ellipse(screen, GREEN, (50, 50, 500, 500), 3)
  3. c1rect = pygame.draw.circle(screen, GOLD, (200, 180), 30, 5)
  4. c2rect = pygame.draw.circle(screen, GOLD, (400, 180), 30)
  5. r1rect = pygame.draw.rect(screen, RED, (170, 130, 60, 10), 3)
  6. r2rect = pygame.draw.rect(screen, RED, (370, 130, 60, 10))
  7. plist = [(295,170), (285,250), (260,280), (340,280), (315,250), (305 ,170)]
  8. l1rect = pygame.draw.lines(screen, GOLD, True, plist, 2)
  9. a1rect = pygame.draw.arc(screen, RED, (200, 220, 200, 100), 1.4*pi, 1.9*pi, 3)
  10. # 3. game loop
  11. while True:
  12. # Process Input
  13. ...
  14. # update game
  15. ...
  16. # render
  17. # 报错的原因:
  18. # (1) 图形绘制与显示只需要指定screen后,1步就可以获取一个pygame.Rect了,无需在blit())
  19. # (2) pygame.Rect 对象即表示图形已经绘制完成,并且可以显示了,无需在blit()块移动;
  20. screen.blit(r1rect, (500, 500))
  21. pygame.display.update()
  1. Traceback (most recent call last):
  2. File "F:/My_Learn/PygameCourses/PYG05/[Error]PYG05-03 多图形绘制.py", line 46, in <module>
  3. screen.blit(r1rect, (500, 500))
  4. TypeError: argument 1 must be pygame.Surface, not pygame.Rect
  5. Process finished with exit code 1
  1. # surface_1: 图像绘制
  2. # 1) 载入图片对象
  3. image = pygame.image.load(r'F:\My_Learn\PygameCourses\images\Ball.png')
  4. # 2) 获取图片对象矩形属性(0,0, 200,200), 默认rect的坐标为(left.top) = (0,0)
  5. image_rect = image.get_rect()
  6. # 3) 图片surface 移动到主图层进行显示
  7. screen.blit(ball, ball_rect)

学习参考:

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注