01event
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: KingJX
# @Date : 2018/7/30 10:44
""""""
import pygame
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((600, 400))
screen.fill((255, 255, 255))
# 设置窗口标题
pygame.display.set_caption('游戏事件')
pygame.display.flip()
"""
QUIT:关闭按钮被点击事件
MOUSEBUTTONDOWN:鼠标按下
"""
while True:
for event in pygame.event.get():
# 不同类型的事件对应的type值不一样
if event.type == pygame.QUIT:
exit()
# 鼠标按下事件
# pos属性,获取鼠标事件产生的位置
if event.type == pygame.MOUSEBUTTONDOWN:
print('鼠标按下', event.pos)
if event.type == pygame.MOUSEBUTTONUP:
print('鼠标弹起', event.pos)
if event.type == pygame.MOUSEMOTION:
print('鼠标移动', event.pos)
# 键盘相关事件
# KEY属性,被按的暗降对应的值得编码
if event.type == pygame.KEYDOWN:
print('键盘按键被按下', chr(event.key))
if event.type == pygame.KEYUP:
print('键盘按键弹起', chr(event.key))
鼠标移动 (376, 286)
鼠标移动 (373, 285)
鼠标移动 (372, 285)
鼠标按下 (372, 285)
鼠标弹起 (372, 285)
键盘按键被按下 f
键盘按键弹起 f
键盘按键被按下 d
键盘按键弹起 d
键盘按键被按下 v
键盘按键弹起 v
鼠标按下 (372, 285)
鼠标弹起 (372, 285)
鼠标按下 (372, 285)
键盘按键被按下 w
键盘按键弹起 w
鼠标弹起 (372, 285)
02-鼠标事件得应用
import pygame
from random import randint
def rand_color():
return randint(0,255),randint(0,255),randint(0,255)
def draw_ball(screen, pos):
pygame.draw.circle(screen, rand_color(), pos, randint(10, 20))
# 只要屏幕上的内容有更新,都需要调用这两个方法中得一个
# pygame.display.flip()
pygame.display.update()
# 写一个函数,判断指定的点,是否在指定的矩形范围中
def isInRect(point, rect):
x, y = point
rx,ry,rw,rh = rect
if (rx<=x<rx+rw) and (ry<=y<=ry+rh):
return True
return False
# 画一个按钮
def draw_button(screen, btn_color, title_color):
# 画个按钮
"""矩形框"""
pygame.draw.rect(screen, btn_color, (100, 100, 100, 60))
"""文字"""
font = pygame.font.SysFont('Times', 30)
title = font.render('clicke', True, title_color)
screen.blit(title, (120, 120))
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((600,400))
screen.fill((255,255,255))
pygame.display.set_caption('鼠标事件')
draw_button(screen,(0,255,0),(255,0,0))
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if isInRect(event.pos,(100,100,100,60)):
draw_button(screen,(0,100,0),(0,0,100))
pygame.display.update()
if event.type == pygame.MOUSEBUTTONUP:
if isInRect(event.pos,(100,100,100,60)):
draw_button(screen,(0,255,0),(255,0,0))
pygame.display.update()
if event.type == pygame.MOUSEMOTION:
screen.fill((255,255,255))
draw_button(screen, (0, 255, 0), (255, 0, 0))
draw_ball(screen,event.pos)
# pygame.display(screen,event.pos)
鼠标点击事件(1)png.png
鼠标点击事件(2)png.png
03鼠标点击事件
import pygame
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
return False
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((800,600))
screen.fill((255,255,255))
img = pygame.image.load('./imges/img1.png')
img_x = 100
img_y = 100
screen.blit(img, (img_x,img_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:
img = pygame.image.load('./imges/img1.png')
x, y = img.get_size()
img_w, img_h = img.get_size()
if is_in_rect(event.pos,(img_x,img_y,img_w,img_h)):
is_move = True
if event.type == pygame.MOUSEBUTTONUP:
is_move = False
if event.type == pygame.MOUSEMOTION:
if is_move:
screen.fill((255, 255, 255))
img = pygame.image.load('./imges/img1.png')
x,y = event.pos
img_w,img_h = img.get_size()
img_x = x-img_w/2
img_y = y-img_h/2
screen.blit(img, (img_x,img_y))
# 3.展示在屏幕上
pygame.display.update()
# pygame.display.update()
拖拽图片.png
04-动画效果
import pygame
from random import randint
def static_page(screen):
# 静态文字
font = pygame.font.SysFont('Times',40)
title = font.render('Hello',True,(255,0,0))
screen.blit(title,(200,200))
def animation_title(screen):
font = pygame.font.SysFont('Times', 40)
title = font.render('Hello', True, (randint(0,255), randint(0,255), randint(0,255)))
screen.blit(title, (100, 100))
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((800,600))
screen.fill((255,255,255))
static_page(screen)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
pygame.time.delay(300)
screen.fill((255, 255, 255))
static_page(screen)
animation_title(screen)
pygame.display.update()
05-ballgame
import pygame
from random import randint
def draw_ball(place, color,pos):
pygame.draw.circle(place,color,pos,20)
Up = 273
Down = 274
Left = 276
Right = 275
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((800,600))
screen.fill((255,255,255))
pygame.display.flip()
ball_x = 400
ball_y = 300
x_speed = 0
y_speed = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if event.key == Up:
y_speed = -1
x_speed = 0
elif event.key == Down:
y_speed = 1
x_speed = 0
elif event.key == Left:
y_speed = 0
x_speed = -1
elif event.key == Right:
x_speed = 1
y_speed = 0
screen.fill((255, 255, 255))
ball_x += x_speed
ball_y += y_speed
# 小球到边界后,自动弹回
if ball_x + 20 >= 800 or ball_x + 20 <= 20:
x_speed *= -1
if ball_y + 20 >= 600 or ball_y + 20 <= 20:
y_speed *=-1
draw_ball(screen,(255,0,0),(ball_x,ball_y))
pygame.display.update()
网友评论