Day11-pygame应用

1.pygame事件

  • QUIT:关闭按钮被点击事件
    MOUSEBUTTONDOWN:鼠标按下
    • MOUSEBUTTONUP:鼠标弹起

    • MOUSEMOTION:鼠标移动

    • 关心鼠标事件产生的位置

    • pos属性,获取鼠标事件产生的位置

    • KEYDOWN:键盘按键被按下

    • KEYUP:键盘按钮弹起
      关心键盘事件产生的位置
      *key属性,获取被按的按键对应的Unicode编码
      *chr,将Unicode转换为字符

import pygame
if __name__ == '__main__':
    pygame.init()
    screen=pygame.display.set_mode((600,400))
    screen.fill((255,255,255))
    # screen.blit()
    # 设置游戏事件
    pygame.display.set_caption('游戏事件')
    pygame.display.flip()
while True:
        #每次循环检查事件
        for event in pygame.event.get():
            #不同类型的事件对应的type值不一样
            # pos属性,获取鼠标事件产生的位置
            if event.type==pygame.QUIT:
                exit()
            if event.type==pygame.MOUSEBUTTONDOWN:
                print('鼠标按下',event.pos)
            if event.type==pygame.MOUSEBUTTONUP:
                print('鼠标弹起',event.pos)
            if event.type==pygame.MOUSEMOTION:
                print('鼠标移动',event.pos)
            #键盘相关事件
            if event.type==pygame.KEYDOWN:
                print('键盘按键被按下',chr(event.key))
            if event.type==pygame.KEYUP:
                print('键盘按钮弹起',chr(event.key))

2.pygame的应用

import pygame
from random import randint
# 画一个按钮
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, 110))
# 写一个函数,判断指定的点是否在指定的矩形范围中
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
def draw_ball(screen,pos):
    pygame.draw.circle(screen, rand_color(), event.pos, randint(10, 20))
    # 只要屏幕上的内容有更新,都需要调用下面的两个方法
    pygame.display.update()
    # pygame.display.flip()
def rand_color():
    return randint(0,255),randint(0,255),randint(0,255)
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:
                # draw_ball(screen,event.pos)
                if is_in_rect(event.pos,(100,100,100,60)):
                    draw_button(screen, (0, 0,255 ), (255, 0, 0))
                    pygame.display.update()
                    print('点中按钮')
            if event.type==pygame.MOUSEBUTTONUP:
                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, 0, 255), (255, 0, 0))
                draw_ball(screen,event.pos)

结果:


闪烁.JPG

3.pygame应用2

'''__author__==fubo'''

'''


'''
import pygame
def get_image(address,pos):
    images = pygame.image.load('./bomb.png')
    x, y = images.get_size()
    x1, y1 = event.pos
    address.blit(images, (x1-int(x/2),y1-int(y/2)))
    return images
def is_in_image(images,pos,num1,num2):
    x,y=images.get_size()
    x1,y1=event.pos
    if (num1<=x1<=x+num1) and (num2

结果


练习.JPG

练习2.JPG

4.动画效果

'''__author__==fubo'''

'''
动画原理:不断的刷新界面上的内容(一帧一帧的画)
'''
import pygame
from random import randint
def static(address):
    font=pygame.font.SysFont('Times',40)
    title=font.render('welcome',True,(255,0,0))
    screen.blit(title,(50,50))
def animation_title(address):
    font = pygame.font.SysFont('Times', 40)
    title = font.render('welcome', 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((600,400))
    screen.fill((255,255,255))
    static(screen)
    pygame.display.flip()
    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                exit()
        # 不断改变字体的颜色
        # 程序执行到这个位置,cpu休息一段时间再执行后面的代码(程序堵塞)
        # 单位:毫秒(1000ms=1s)
        pygame.time.delay(160)
        # 内容清空
        screen.fill((255,255,255))
        static(screen)
        animation_title(screen)
        # pygame.time.wait(1000)
        # 内容展示
        pygame.display.update()

结果:

5.球的移动

'''__author__==fubo'''

'''


'''

import pygame
def draw_ball(place,color,pos):
    '''画球'''
    pygame.draw.circle(place,color,pos,20)

if __name__ == '__main__':
    pygame.init()
    screen=pygame.display.set_mode((600,400))
    screen.fill((255,255,255))
    pygame.display.flip()
    # 保存初始坐标
    ball_x=100
    ball_y=100
    Up = 273
    Down = 274
    Left = 276
    Right = 275
    x_speed=1
    y_speed=1
    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=-5
                    x_speed=0
                elif event.key==Down:
                    y_speed=5
                    x_speed=0
                elif event.key==Left:
                    y_speed=0
                    x_speed=-5
                elif event.key==Right:
                    y_speed=0
                    x_speed=5
                # 刷新屏幕
        screen.fill((255,255,255))
        ball_x+=x_speed
        ball_y+=y_speed
        if ball_x+20>=600:
            ball_x=600-20
            x_speed=-5
        if ball_x-20<=0:
            x_speed=5
        if ball_y + 20 >= 400:
            ball_y = 400 - 20
            y_speed = -5
        if ball_y - 20 <= 0:
            ball_y=20
            y_speed = 5
        pygame.time.delay(200)
        draw_ball(screen,(255,0,0),(ball_x,ball_y))
        pygame.display.update()

结果:


球移动1.JPG

球移动2.JPG

你可能感兴趣的:(Day11-pygame应用)