pygmame 封装了一些通用的游戏操作: 鼠标键盘图片音乐 …
pip3 install pygame
游戏的框架
import pygame
#from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('测试游戏')
clock = pygame.time.Clock()
#三原色 rgb 红绿蓝,每种颜色的取值为 0-255
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
while True:
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
pygame.display.update()
clock.tick(10)
画各种常见图形
画矩形
import pygame
#from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('测试游戏')
clock = pygame.time.Clock()
#三原色 rgb 红绿蓝,每种颜色的取值为 0-255
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
while True:
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
#方法一
# pygame.draw.rect(screen,RED,(200,100,300,200),2)
# pygame.draw.rect(screen,RED,(200,100,300,200))
#方法二
sur = pygame.Surface((300,200))
sur.fill(RED)
screen.blit(sur,(200,200))
pygame.display.update()
clock.tick(10)
画其它图形方法
import pygame
#from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('测试游戏')
clock = pygame.time.Clock()
#三原色 rgb 红绿蓝,每种颜色的取值为 0-255
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
while True:
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
#多边形
# pygame.draw.polygon(screen,RED,((200,200),(300,400),(600,400),(400,100)))
# pygame.draw.polygon(screen,RED,((200,200),(300,400),(600,400),(400,100)),3)
#画圆
# pygame.draw.circle(screen,RED,(400,300),100,2)
#画椭圆
# pygame.draw.ellipse(screen,RED,(200,100,300,200))
#画弧形,一个圆是360角度或2pi弧度,这里的开始和结束使用的是弧度
# pygame.draw.arc(screen,RED,(200,100,300,200),0,3.14)
#画单根线
# pygame.draw.line(screen,RED,(200,200),(600,400),10)
# pygame.draw.aaline(screen,RED,(100,100),(700,500),10)
# 画多根线
# pygame.draw.lines(screen,RED,False,((200,200),(300,400),(600,400),(400,100)),2)
# pygame.draw.aalines(screen,RED,True,((200,200),(300,400),(600,400),(400,100)),2)
#得到某一个点上的色值
# color = screen.get_at((100,100))
# print(color)
#点n个点
x,y = 100,100
for i in range(10):
x += 10
y += 10
screen.set_at((x,y),RED)
pygame.display.update()
clock.tick(10)