Pygame(六)动画起步1

本文介绍了Pygame中实现动画的基础,通过不断更新画面实现动态效果。讲解了`move`和`move_ip`两个方法的参数及用法,用于处理对象的移动,并通过示例代码展示它们的区别。文章最后提出了一个作业,要求设计一个能在碰到墙壁时反弹的pygame程序。
摘要由CSDN通过智能技术生成

Pygame(六)动画起步1

前情提要

image.png

作业示例

image.png

# /usr/bin/python3

# Author: 爱编程的章老师
# @Time: 2021/1/4 0004
# E-mail: Bluesand2010@163.com


import pygame
import sys


def homework():
    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    screen.fill((255,255,255))
    # 画房子主体
    rect = pygame.Rect(260,390,280,210)
    c = 102, 204, 255
    pygame.draw.rect(screen, c, rect, 0)

    # 画窗户:边框 加两条张
    rect = pygame.Rect(285,420,70,70)
    c = 102, 204, 153
    BLACK = 0,0,0
    h_line_start = 285, 455
    h_lint_end = 354, 455
    v_line_start = 320, 420
    v_lint_end = 320, 489
    pygame.draw.rect(screen, c, rect, 0)
    pygame.draw.rect(screen, BLACK, rect, 1) # 画黑色边框
    pygame.draw.line(screen, BLACK, h_line_start, h_lint_end, 1)
    pygame.draw.line(screen, BLACK, v_line_start, v_lint_end, 1)


    # 画门
    rect = pygame.Rect(445,520,55,80)
    c = 255, 153, 51
    pygame.draw.rect(screen, c, rect, 0)

    # 画门把手
    pos = 490, 570
    r = 6
    c = 102, 102, 102
    pygame.draw.circle(screen, c, pos,<
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
PygamePython 中用于开发游戏的库,它可以用来创建和播放简单的动画。要使用 Pygame 播放动画,你需要做以下几步: 1. **导入模块**: 首先,确保已经安装了 pygame,如果没有,可以通过 `pip install pygame` 来安装。然后,在你的代码开始时导入必要的模块: ```python import pygame from pygame.locals import * ``` 2. **初始化 Pygame**: 在开始操作之前,你需要初始化 pygame: ```python pygame.init() ``` 3. **设置窗口**: 创建一个窗口,这是动画显示的地方: ```python screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Animation") ``` 4. **加载图片**: 使用 `pygame.image.load()` 函数加载动画序列中的每一帧图片。例如,假设你有三个帧的图像文件(frame0.png, frame1.png, frame2.png): ```python images = [pygame.image.load('frame{}.png'.format(i)) for i in range(3)] ``` 5. **定时器或循环**: 创建一个循环来轮流显示每个帧,你可以使用 `pygame.time.wait()` 或者 `pygame.event.get()` 结合一个 while 循环来控制帧率。这可能涉及到使用一个变量来跟踪当前帧的位置: ```python current_frame = 0 clock = pygame.time.Clock() running = True while running: for event in pygame.event.get(): if event.type == QUIT: running = False screen.blit(images[current_frame], (0, 0)) pygame.display.flip() # 更新帧并检查是否到达最后一帧 current_frame = (current_frame + 1) % len(images) clock.tick(10) # 控制每秒多少帧 ``` 6. **关闭窗口**: 当游戏结束时,记得关闭窗口: ```python pygame.quit() ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱编程的章老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值