1.pygame事件
QUIT:关闭按钮被点击事件
if event.type == pygame.QUIT:
exit()
MOUSEBUTTONDOWN:鼠标按下事件
MOUSEBUTTONUP:鼠标弹起事件
MOUSEMOTION:鼠标移动事件
# 鼠标相关事件
if event.type == pygame.MOUSEBUTTONDOWN:
print('鼠标按下')
if event.type == pygame.MOUSEBUTTONUP:
print('鼠标弹起')
if event.type == pygame.MOUSEMOTION:
print('鼠标移动')
KEYDOWN:键盘按键被按下事件
KEYUP:键盘按钮弹起事件
# 键盘相关事件
if event.type == pygame.KEYDOWN:
print('键盘按键被按下', chr(event.KEY))
if event.type == pygame.KEYUP:
print('键盘按钮弹起', chr(event.KEY))
2.鼠标事件的应用
2.1.鼠标点击的地方画圆
import pygame
from random import randint
def rand_color():
"""
产生随机颜色
"""
return (randint(1, 255), randint(1, 255), randint(1, 255))
def draw_circle(screen, pos):
"""
在鼠标点击的位置画球
"""
pygame.draw.circle(screen, rand_color(), pos, randint(10, 40))
# 只要屏幕上的内容有更新,都需要调用下面这两个方法中的一个
# pygame.display.flip()
pygame.display.update()
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((600, 400))
screen.fill((254, 254, 254))
pygame.display.set_caption('鼠标事件')
# 画个按钮
draw_button(screen, (0, 254, 0), (254, 0, 0))
pygame.display.flip()
while True:
for event in pygame.event.get():
# 退出
if event.type == pygame.QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
# 在指定坐标画圆
draw_circle(screen, event.pos)
2.2.画按钮并实现点击变色功能
# 写一个函数,判断指定的点是否在矩形框中
def is_in_rect(point, rect):
x, y = point
rx, ry, rw, rh = rect
if rx <= x <= (rx + rw) and ry <= y <= (ry + rh):
return True
# 写一个函数,画一个按钮
def draw_button(screen, btn_color, title_color):
# 画个按钮
"""矩形框"""
pygame.draw.rect(screen, btn_color, (100, 100, 100, 60))
"""文字"""
font = pygame.font.SysFont('Times', 20)
title = font.render('button', True, title_color)
screen.blit(title, (120, 120))
if __name__='__main__':
... #重复pygame中的步骤
while True:
for event in pygame.event.get():
# 退出
if event.type == pygame.QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
# 在指定坐标画圆
# draw_circle(screen, event.pos)
if is_in_rect(event.pos, (100, 100, 100, 60)):
draw_button(screen, (0, 200, 0), (100, 0, 0))
pygame.display.flip()
# 鼠标点击按钮
if event.type == pygame.MOUSEBUTTONUP:
if is_in_rect(event.pos, (100, 100, 100, 60)):
draw_button(screen, (0, 254, 0), (254, 0, 0))
pygame.display.update()
2.3.鼠标移动画圆
if event.type == pygame.MOUSEMOTION:
screen.fill((254, 254, 254))
draw_button(screen, (0, 254, 0), (254, 0, 0))
draw_circle(screen, event.pos)
2.3.鼠标拖拽
import pygame
# 要求:先在屏幕上显示一张图片,鼠标按下移动的时候,拽着图片跟着鼠标移动,鼠标弹起后就不动了
# 写一个函数,判断点是否在某个范围内
def is_in_rect(pos, rect):
x, y = pos
rx, ry, rw, rh = rect
if (rx <= x <= (rx + rw)) and (ry <= y <= (ry + rh)):
return True
return False
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((1000, 800))
screen.fill((254, 254, 254))
pygame.display.set_caption('图片拖拽')
image = pygame.image.load('./lufei.jpg')
image_x, image_y = 0, 0
screen.blit(image, (image_x, image_y))
pygame.display.flip()
# 用来存储图片是否可以移动
is_move = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
# 鼠标按下,让状态变成可以移动
if event.type == pygame.MOUSEBUTTONDOWN:
w, h = image.get_size()
if is_in_rect(event.pos, (image_x, image_y, w, h)):
is_move = True
# 鼠标弹起,让状态变成不可以移动
if event.type == pygame.MOUSEBUTTONUP:
is_move = False
if event.type == pygame.MOUSEMOTION:
if is_move:
screen.fill((254, 254, 254))
x, y = event.pos
image_w, image_h = image.get_size()
image_x = x-image_w/2
image_y = y-image_h/2
screen.blit(image, (x-image_w/2, y-image_h/2))
pygame.display.update()
3.动画效果
动画原理:不断的刷新界面上的内容(一帧一帧的画)
import pygame
from random import randint
def static_page(screen):
"""
页面上的静态内容
"""
# 静态文字
font = pygame.font.SysFont('Times', 40)
title = font.render('Welcome', True, (254, 0, 0))
screen.blit(title, (100, 200))
def animation_title():
"""
字体颜色发生改变
"""
font = pygame.font.SysFont('Times', 40)
title = font.render('Python', True, (randint(1, 255), randint(1, 255), randint(1, 255)))
screen.blit(title, (280, 200))
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((600, 400))
screen.fill((254, 254, 254))
static_page(screen)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
# 在下面去写每一帧要显示的内容
"""
程序执行到这个位置,线程在这阻塞指定的时间
单位:毫秒 (1000ms = 1s)
"""
pygame.time.delay(1000)
# 动画前清空原来的内容
screen.fill((254, 254, 254))
static_page(screen)
animation_title()
pygame.display.update()
多个球一起动
"""__author__=xx"""
import pygame
from random import randint
from math import sqrt
# 写一个函数,判断两个球是否相遇
def is_ball_meet(ball_1, ball_2):
x1, y1 = ball_1['pos']
r1 = ball_1['r']
x2, y2 = ball_2['pos']
r2 = ball_2['r']
if (r1 + r2) > sqrt((x1-x2)**2 + (y1-y2)**2):
return True
return False
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((600, 400))
screen.fill((254,254,254))
pygame.display.flip()
# all_balls中保存多个球
# 每个球要保存:半径、圆心、颜色、x速度、y速度
all_balls = []
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
ball_dict1 = {'r':randint(10,30), 'pos':event.pos, 'color':(randint(1,255),randint(1,255),randint(1,255)), 'x_speed':randint(-5,5), 'y_speed':randint(-5,5)}
all_balls.append(ball_dict1)
# 刷新界面
screen.fill((254,254,254))
pygame.time.delay(100)
for ball_dict in all_balls:
# 取出球原来的x坐标和y坐标以及速度
x,y = ball_dict['pos']
x_speed = ball_dict['x_speed']
y_speed = ball_dict['y_speed']
x += x_speed
y += y_speed
r = ball_dict['r']
if x - r <= 0:
x = r
x_speed = 5
if x + r >= 600:
x = 600 - r
x_speed = -5
if y - r <= 0:
y = r
y_speed = 7
if y + r >= 400:
y = 400 - r
y_speed = -7
pygame.draw.circle(screen,ball_dict['color'],(x,y),r)
# 更新球对应的坐标
ball_dict['pos'] = x,y
ball_dict['x_speed'] = x_speed
ball_dict['y_speed'] = y_speed
pygame.display.flip()