import pygame
"""
1.鼠標(biāo)事件:
事件類型:event.type
MOUSEBUTTONDOWN --> 鼠標(biāo)按下
MOUSEBUTTONUP? --> 鼠標(biāo)彈起
MOUSEMOTION --> 鼠標(biāo)移動
關(guān)心鼠標(biāo)的位置:event.pos
鍵盤事件
"""
import random
def rand_color():
? ? """隨機顏色"""
? ? return random.randint(0,255), random.randint(0,255), random.randint(0,255)
pygame.init()
screen= pygame.display.set_mode((600,400))
screen.fill((255,255,255))
pygame.display.flip()
while True:
? ? # 只要有事件產(chǎn)生就會進入for循環(huán)
? ? for eventin pygame.event.get():
? ? ? ? # 根據(jù)判斷type的值來判斷是什么事件產(chǎn)生了
? ? ? ? if event.type== pygame.QUIT:
? ? ? ? ? ? exit()
# =================鼠標(biāo)事件=================
? ? ? ? elif event.type== pygame.MOUSEBUTTONDOWN:
? ? ? ? ? ? # 鼠標(biāo)按下后要做什么事情就寫在這兒...
? ? ? ? ? ? print('鼠標(biāo)按下:', event.pos)
pygame.draw.circle(screen, rand_color(), event.pos, random.randint(10,40))
pygame.display.flip()
elif event.type== pygame.MOUSEBUTTONUP:
? ? ? ? ? ? # 鼠標(biāo)按下后彈起
? ? ? ? ? ? print('鼠標(biāo)彈起', event.pos)
elif event.type== pygame.MOUSEMOTION:
? ? ? ? ? ? # 鼠標(biāo)移動
? ? ? ? ? ? print('鼠標(biāo)移動', event.pos)
# pygame.draw.circle(screen, rand_color(), event.pos, 30)
# pygame.display.flip()
# ==================鍵盤事件======================
? ? ? ? elif event.type== pygame.KEYDOWN:
? ? ? ? ? ? print('按鍵按下:', event.key,chr(event.key))
elif event.type== pygame.KEYUP:
? ? ? ? ? ? print('按鍵彈起:', event.key,chr(event.key))
////////////////////////////////
import pygame
from mathimport pi
if __name__== '__main__':
? ? # 初始化,創(chuàng)建窗口
? ? pygame.init()
window= pygame.display.set_mode((400,600))
window.fill((255,255,255))
"""
1.畫線段
def line(Surface, color, start_pos, end_pos, width=1)
Surface: 畫在哪兒
color:線的顏色
start_pos: 起點
end_pos:終點
width: 線寬
"""
? ? # 畫一條水平線
? ? pygame.draw.line(window, (255,0,0), (50,100), (200,100))
# 畫一條垂直線
? ? pygame.draw.line(window, (0,255,0), (50,100), (50,200),2)
"""
2.畫線段(折線)
def lines(Surface, color, closed, pointlist, width=1)
Surface: 畫在哪兒
color: 線的顏色
closed: 是否閉合(是否連接起點和終點)
pointlist:點對應(yīng)的列表
"""
? ? pygame.draw.lines(window, (0,0,255),True, [(100,200), (150,120), (140,300)])
"""
3.畫圓
def circle(Surface, color, pos, radius, width=0)
Surface: 畫在哪兒
color: 顏色
pos:圓心坐標(biāo)
radius:半徑
width: 線寬律罢,0 -> 填充
"""
? ? pygame.draw.circle(window, (255,255,0), (200,300),100,0)
"""
4.畫矩形
def rect(Surface, color, Rect, width=0)
Surface:畫在哪兒
color: 顏色
Rect:范圍(元祖咧党,元祖中有四個元素紧憾,分別是x,y,width,height)
"""
? ? pygame.draw.rect(window, (0,255,0), (10,100,50,100))
"""
5.畫多邊形
polygon(Surface, color, pointlist, width=0)
"""
? ? pygame.draw.polygon(window, (0,255,255), [(300,50), (250,40),(100,50), (200,150)])
"""
6.畫橢圓
def ellipse(Surface, color, Rect, width=0)
"""
? ? pygame.draw.ellipse(window, (123,200,210), (10,200,150,60))
"""
7.畫弧線
def arc(Surface, color, Rect, start_angle, stop_angle, width=1)
start_angle: 0-2pi
stop_angle:
pi --- 180°? 1° --- pi/180
59° = pi/180 * 59
"""
? ? pygame.draw.arc(window, (255,0,0), (200,400,100,100), pi/4+pi, pi*3/4+pi,3)
# 展示內(nèi)容
? ? pygame.display.flip()
# 游戲循環(huán)
? ? while True:
? ? ? ? for eventin pygame.event.get():
? ? ? ? ? ? if event.type== pygame.QUIT:
? ? ? ? ? ? ? ? exit()