栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python基础:第011课——最小的程序框架代码

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python基础:第011课——最小的程序框架代码

index
  • Python基础:第011课——最小的程序框架代码
    • 下载程序中用到的图像
    • 笛卡尔屏幕坐标系
    • 最小的程序框架代码
    • 本次课完成的代码

Python基础:第011课——最小的程序框架代码

我的授课方法与传统教学不同,要能继续学下去,需要:

  1. 对前面的课程能熟练掌握
  2. 本次课的代码多练习几次
  3. 即使有些代码不明白,可以先照着写,次数多了,就会明白。

观看视频

下载程序中用到的图像

在下面图像上右键单击,选择“将图像另存为”,就可以下载了

笛卡尔屏幕坐标系

最小的程序框架代码
import sys, pygame

pygame.init()
screen = pygame.display.set_mode((600,400))
pygame.display.set_caption("星火工坊")

ball = pygame.image.load("pygame/images/ball.gif")

ball_rect = ball.get_rect()

speed = [1, 1]
while True:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            sys.exit()

    if ball_rect.top < 0 or ball_rect.bottom > 400:
        speed[1] = -speed[1]

    if ball_rect.left < 0 or ball_rect.right > 600:
        speed[0] = -speed[0]
        
    screen.fill((0,0,0))
    ball_rect = ball_rect.move(speed[0], speed[1])
    screen.blit(ball, ball_rect)

    pygame.display.update()

本次课完成的代码
import pygame, sys, os

pygame.init()

screen = pygame.display.set_mode((600,400))
pygame.display.set_caption("小小工坊")

ball = pygame.image.load('pygame/images/ball.gif')

ball_rect = ball.get_rect()
speed = 1
fclock = pygame.time.Clock()

while True :
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    ball_rect = ball_rect.move(speed,speed)
    screen.fill((0,0,0))
    screen.blit(ball,ball_rect)

    pygame.display.update()
    fclock.tick(60)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/968409.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号