pygame模块补充
import pygame
from random import randint as rdi
def randcol():
return (rdi(0, 255), rdi(0, 255), rdi(0, 255))
def main():
pygame.init()
# 设置标题
pygame.display.set_caption('event')
screen = pygame.display.set_mode((700, 350))
screen.fill((255, 255, 255))
pygame.display.flip()
'''
QUIT:关闭按钮被点击事件
MOUSEBUTTONDOWN:鼠标按下事件
MOUSEBUTTONUP:鼠标弹起事件
MOUSEMOTION:鼠标移动事件
KEYDOWN:键盘按下
KEYUP:键盘弹起
'''
while True:
# 每次循环检测有没有事件发生
clo = (rdi(0, 255), rdi(0, 255), rdi(0, 255))
for evt in pygame.event.get():
# 不同类型的事件对应得type值不一样
if evt.type == pygame.QUIT:
exit()
if evt.type == pygame.MOUSEBUTTONDOWN:
screen.fill(clo)
pygame.display.flip()
if evt.type == pygame.MOUSEBUTTONUP:
print('mouseup')
if evt.type == pygame.MOUSEMOTION:
pass
# print(evt.pos)
# 键盘相关事件
# key属性,被按的按键对应的ASCII码
if evt.type == pygame.KEYDOWN:
print('keydown', evt.key)
if evt.type == pygame.KEYUP:
print('keyup', chr(evt.key))
if __name__ == '__main__':
main()
鼠标应用事件1
import pygame
from random import randint as rdi
def randcol():
return (rdi(0, 255), rdi(0, 255), rdi(0, 255))
def drawCircle(screen, pos):
pygame.draw.circle(screen, randcol(), pos, rdi(10, 50))
pygame.display.update()
def isInRect(point, rect):
x, y = point
rx, ry, width, height = rect
if rx <= x <= rx + width and ry <= y <= ry + height:
return True
return False
# 画个按钮
def drawButton(screen, btnColor, titleColor, text1):
pygame.draw.rect(screen, btnColor, (100, 100, 80, 50))
font = pygame.font.SysFont('华文楷体', 20)
title = font.render(text1, True, titleColor)
screen.blit(title, (120, 110))
pygame.display.update()
def main():
pygame.init()
screen = pygame.display.set_caption('鼠标点击事件')
screen = pygame.display.set_mode((600, 400))
screen.fill((255, 255, 255))
drawButton(screen, (85, 58, 85), (25, 52, 25), '流批')
pygame.display.flip()
while True:
for evt in pygame.event.get():
if evt.type == pygame.QUIT:
exit()
if evt.type == pygame.MOUSEBUTTONDOWN:
# 指定坐标画圆
# drawCircle(screen, evt.pos)
if isInRect(evt.pos, (100, 100, 80, 50)):
drawButton(screen, (200, 58, 201), (45, 54, 25), '流批')
if evt.type == pygame.MOUSEBUTTONUP:
# 指定坐标画圆
# drawCircle(screen, evt.pos)
if isInRect(evt.pos, (100, 100, 80, 50)):
drawButton(screen, (33, 33, 33), (123, 12, 32), '流批')
if evt.type == pygame.MOUSEMOTION:
screen.fill((255, 255, 255))
drawButton(screen, (33, 33, 33), (123, 12, 32), '流批')
drawCircle(screen, evt.pos)
pygame.display.update()
if __name__ == '__main__':
main()
鼠标应用事件2
# 屏幕显示图片 鼠标按下拖动图片,鼠标弹起就不动
import pygame
def isInImg(point, rect):
x, y = point
rx, ry, width, height = rect
if rx <= x <= rx + width and ry <= y <= ry + height:
return True
return False
def imgMove(img, sc, point):
sc.blit(img, point)
def main():
pygame.init()
sc = pygame.display.set_caption('mouse2')
sc = pygame.display.set_mode((600, 400))
sc.fill((255, 255, 255))
img = pygame.image.load('./day11/img/xin.jpeg')
img = pygame.transform.scale(img, (50, 50))
imgX, imgY = 50, 50
sc.blit(img, (imgX, imgY))
pygame.display.flip()
canMove = False
w, h = img.get_size()
while True:
for evt in pygame.event.get():
if evt.type == pygame.QUIT:
exit()
if evt.type == pygame.MOUSEMOTION:
if canMove:
sc.fill((255, 255, 255))
x, y = evt.pos
imgX = x - w / 2
imgY = y - h / 2
imgMove(img, sc, (imgX, imgY))
pygame.display.update()
if evt.type == pygame.MOUSEBUTTONDOWN:
if isInImg(evt.pos, (imgX, imgY, 50, 50)):
canMove = True
if evt.type == pygame.MOUSEBUTTONUP:
canMove = False
pygame.display.update()
if __name__ == '__main__':
main()
动画效果
import pygame
from random import randint as ri
def randcol():
return (ri(0, 255), ri(0, 255), ri(0, 255))
def staticPage(sc):
font = pygame.font.SysFont('华文行楷', 40)
title = font.render('流批', True, (0, 0, 0))
sc.blit(title, (200, 200))
def animatePage(sc):
font = pygame.font.SysFont('黑体', 40)
title = font.render('PY', True, randcol())
sc.blit(title, (100, 100))
def main():
'''
原理:不断刷新界面的内容
'''
pygame.init()
screen = pygame.display.set_caption('动画效果')
screen = pygame.display.set_mode((900, 400))
screen.fill((255, 255, 255))
pygame.display.flip()
staticPage(screen)
while True:
# for循环里面的代码只有事件发生后才会执行
for evt in pygame.event.get():
if evt.type == pygame.QUIT:
exit()
# 线程在此阻塞的时间单位毫秒
pygame.time.delay(200)
# 写自动变化显示的内容
# 开始之前清空屏幕的内容
screen.fill((255, 255, 255))
staticPage(screen)
animatePage(screen)
# 把展示的内容显示出来
pygame.display.update()
if __name__ == '__main__':
main()
小游戏1
'''
@Author: Kris Shin
@Date: 2018-07-30 15:39:45
@Last Modified by: Kris Shin
@Last Modified time: 2018-07-30 15:39:45
'''
import pygame
import time # 导入时间函数
# 保存按键的ASCII码
UP = 273
DOWN = 274
RIGHT = 275
LEFT = 276
# 封装画圆函数
def drawBall(sc, clr, point):
pygame.draw.circle(sc, clr, point, 15)
def main():
pygame.init()
screen = pygame.display.set_caption('ball')
screen = pygame.display.set_mode((800, 600))
screen.fill((255, 255, 255))
# save position
ballX = 100
ballY = 100
speed = 2 # 初始速度
x = 1 # 设置默认方向
y = 0
pygame.display.flip()
stTime = time.time() # 获取当前时间作为开始时间
while True:
edTime = time.time() # 获取当前时间节点
if edTime - stTime > 3: # 每3秒速度+1
speed += 1
stTime = edTime # 更新开始时间
for evt in pygame.event.get():
if evt.type == pygame.QUIT: # 退出按钮
exit()
if evt.type == pygame.KEYDOWN: # 键盘上下左右按键事件
if evt.key == UP:
x = 0 # 控制只能延x/y轴运行,否则控制很困难
y = -1
elif evt.key == DOWN:
x = 0
y = 1
elif evt.key == LEFT:
y = 0
x = -1
elif evt.key == RIGHT:
y = 0
x = 1
screen.fill((255, 255, 255))
pygame.time.delay(20) # 设置延迟,控制速度
ballX += x * speed # 更新速度
ballY += y * speed
drawBall(screen, (255, 0, 0), (ballX, ballY))
pygame.display.update()
if ballX + 20 > 800 or ballX - 20 < 0 or ballY - 20 < 0 or ballY + 20 > 600: # 如果碰到边缘则结束游戏
screen.fill((255, 255, 255))
font = pygame.font.SysFont('Times', 100, bold=1)
title = font.render("Game Over", True, (255, 0, 0))
screen.blit(title, (150, 200))
pygame.display.update()
time.sleep(3)
exit()
if __name__ == '__main__':
main()
很多小球的作业
import pygame
from random import randint as ri
def randClr():
return (ri(0, 255), ri(0, 255), ri(0, 255))
def rand(x, y):
z1 = ri(x, y)
z2 = ri(x, y)
if z1 or z2:
rand(x, y)
return z1, z2
def gameOver(sc):
sc.fill((255, 255, 255))
font = pygame.font.SysFont('华文隶书', 120)
title = font.render('Game Over!', True, (255, 0, 0))
sc.blit(title, (200, 220))
pygame.display.update()
def main():
pygame.init()
screen = pygame.display.set_caption('Balls')
screen = pygame.display.set_mode((900, 600))
screen.fill((255, 255, 255))
pygame.display.flip()
xS, yS = rand(-3, 3)
balls = [{
'r': ri(8, 15),
'pos': (ri(0, 899), ri(0, 599)),
'color': randClr(),
'xSpeed': xS,
'ySpeed': yS
}, {
'r': ri(8, 15),
'pos': (ri(0, 899), ri(0, 599)),
'color': randClr(),
'xSpeed': xS,
'ySpeed': yS
}]
while True:
for evt in pygame.event.get():
if evt.type == pygame.QUIT:
exit()
if evt.type == pygame.MOUSEBUTTONDOWN:
xS, yS = rand(-3, 3)
nBall = {
'r': ri(8, 15),
'pos': evt.pos,
'color': randClr(),
'xSpeed': xS,
'ySpeed': yS
}
balls.append(nBall)
screen.fill((255, 255, 255))
for ballDict in balls:
r = ballDict['r']
if r == 0:
del ballDict
continue
elif r > 300:
gameOver(screen)
pygame.time.delay(3000)
exit()
# 取出坐标和速度
x, y = ballDict['pos']
xSpeed = ballDict['xSpeed']
ySpeed = ballDict['ySpeed']
x += xSpeed
y += ySpeed
pygame.draw.circle(screen, ballDict['color'], (x, y),
ballDict['r'])
if (x - ballDict['r']) < 0:
x = ballDict['r']
ballDict['xSpeed'] *= -1
if (x + ballDict['r']) > 900:
x = 900 - ballDict['r']
ballDict['xSpeed'] *= -1
if (y - ballDict['r']) < 0:
y = ballDict['r']
ballDict['ySpeed'] *= -1
if (y + ballDict['r']) > 600:
y = 600 - ballDict['r']
ballDict['ySpeed'] *= -1
ballDict['pos'] = x, y
# for ballDict in balls:
for ballDict1 in balls:
if balls.index(ballDict) != balls.index(ballDict1):
if ((ballDict['pos'][0] - ballDict1['pos'][0])**2 +
(ballDict['pos'][1] - ballDict1['pos'][1])**2) <= (
ballDict['r'] + ballDict1['r'])**2:
if ballDict['r'] > ballDict1['r']:
ballDict['r'] += ballDict1['r'] // 2
ballDict1['r'] = 0
else:
ballDict1['r'] += ballDict['r'] // 2
ballDict['r'] = 0
pygame.display.update()
if __name__ == '__main__':
main()