pygame入門練習

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()。否則會非正常結束。