用户交互----进入游戏
一、增加交互----点击和拖动
1、点击鼠标画点的程序
设置
import pygame # Setup
pygame.init()
screen = pygame.display.set_mode([800,600])
pygame.display.set_caption("单击画圆点")
keep_going = True
RED = (255,0,0) # RGB color triplet for RED
radius = 15
在游戏循环中处理鼠标点击事件
while keep_going: # Game loopfor event in pygame.event.get(): # Handling events if event.type == pygame.QUIT: keep_going = Falseif event.type == pygame.MOUSEBUTTONDOWN:spot = event.pospygame.draw.circle(screen, RED, spot, radius)
更新屏幕以及退出时要干嘛并整合
# ClickDots.py
import pygame # Setup
pygame.init()
screen = pygame.display.set_mode([800,600])
pygame.display.set_caption("单击画圆点")
keep_going = True
RED = (255,0,0) # RGB color triplet for RED
radius = 15
while keep_going: # Game loopfor event in pygame.event.get(): # Handling events if event.type == pygame.QUIT: keep_going = Falseif event.type == pygame.MOUSEBUTTONDOWN:spot = event.pospygame.draw.circle(screen, RED, spot, radius)pygame.display.update() # Update displaypygame.quit() # Exit
2、拖动绘制连续的圆点的程序
拖动的特点是鼠标在按下的同时鼠标在移动。
该程序的总体结构与上面相同,故直接给出代码:
# DragDots.py
import pygame # Setup
pygame.init()
screen = pygame.display.set_mode([800,600])
pygame.display.set_caption("Click and drag to draw")
keep_going = True
YELLOW = (255,255,0) # RGB color triplet for YELLOW
radius = 15
mousedown = False
while keep_going: # Game loopfor event in pygame.event.get(): # Handling eventsif event.type == pygame.QUIT: keep_going = Falseif event.type == pygame.MOUSEBUTTONDOWN:mousedown = Trueif event.type == pygame.MOUSEBUTTONUP:mousedown = Falseif mousedown: # Draw/update graphicsspot = pygame.mouse.get_pos()pygame.draw.circle(screen, YELLOW, spot, radius)pygame.display.update() # Update displaypygame.quit() # Exit