赞
踩
直线
line(surface, color, start_pos, end_pos) -> Rect
line(surface, color, start_pos, end_pos, width=1) -> Rect
连续的多条线
lines(surface, color, closed, points) -> Rect
lines(surface, color, closed, points, width=1) -> Rect
圆
circle(surface, color, center, radius) -> Rect
circle(surface, color, center, radius, width=0, draw_top_right=None, draw_top_left=None, draw_bottom_left=None, draw_bottom_right=None) -> Rect
多边形
polygon(surface, color, points) -> Rect
polygon(surface, color, points, width=0) -> Rect
五子棋很简单,先绘制网格线,然后绘制圆就行了
import pygame import time pygame.init() WIDTH,HEIGHT=600,600 screen = pygame.display.set_mode((WIDTH,HEIGHT)) pygame.display.set_caption("这是一个给我们画画用的窗口") clock = pygame.time.Clock() nrows,ncols=10,10 dx = WIDTH // nrows dy = HEIGHT // ncols radius = dx // 4 going = True while going: for event in pygame.event.get(): # 遍历事件 if event.type == pygame.QUIT: # 退出事件 going=False pygame.draw.circle(screen,(255,0,0),(dx*2,dy*2), radius) pygame.draw.circle(screen,(255,0,0),(dx*5,dy*4), radius) for i in range(1, nrows): # 绘制横线 pygame.draw.line(screen,(255,0,0),(0+dx,i*dy),(WIDTH-dx, i*dy)) for j in range(1, ncols): # 绘制纵线 pygame.draw.line(screen,(255,0,0),(j*dx,0+dy),(j*dx, HEIGHT-dy)) pygame.display.update() clock.tick(10) pygame.quit()
加入鼠标点击事件,点击左键就落子,即绘制一个圆
for event in pygame.event.get(): # 遍历事件
if event.type == pygame.QUIT: # 退出事件
going=False
elif event.type == pygame.MOUSEBUTTONDOWN:
x,y=event.pos # 获取当前鼠标的位置
pygame.draw.circle(screen, (255, 255, 0), (x, y), radius)
由于是手动点击的,因此很难正好落在交叉点,可以判断点击位置距离哪个交叉点最近。我们直接算距离哪个位置近即可
for event in pygame.event.get(): # 遍历事件
if event.type == pygame.QUIT: # 退出事件
going=False
elif event.type == pygame.MOUSEBUTTONDOWN:
x,y=event.pos
x = ((x + dx//2) // dx)*dx
y = ((y + dy//2) // dy)*dy
pygame.draw.circle(screen, (255, 255, 0), (x,y), radius)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。