pygame入门练习

这篇博客主要介绍了如何使用pygame库进行图形绘制,包括画圆和矩形的基础操作,并通过一个官方的动画示例展示了pygame的动态效果。特别强调在程序退出时,应使用pygame.quit()确保正常结束。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.画圆和矩形


import pygame
pygame.init()

screen = pygame.display.set_mode([640,480])
pygame.display.set_caption('Draw')


while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: pygame.quit()
    screen.fill([255,255,0])
    pygame.draw.circle(screen,[0,255,0],[100,100],30,0)
    pygame.draw.rect(screen,[0,0,255],[250,150,300,200],0)
    pygame.display.flip()


2.一个官方动画示例

import sys, pygame
pygame.init()

size = width, height = 640, 480
speed = [1, 1]
black = 255, 0, 0
 
screen = pygame.display.set_mode(size)
 
ball = pygame.image.load("ball.gif")
ballrect = ball.get_rect()

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: pygame.quit()

    ballrect = ballrect.move(speed)
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    screen.fill(black)
    screen.blit(ball, ballrect)
    pygame.display.flip()



没能做成动态的,见谅


注意:退出时是调用pygame,quit(),而不是sys.exit()。否则会非正常结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值