美文网首页
day11 pygame

day11 pygame

作者: 马里奥兄弟最爱打饱嗝 | 来源:发表于2018-07-30 20:37 被阅读0次

鼠标事件

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 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_button(screen, bth_color, title_color):
    # 画个按钮
    """矩形框"""
    pygame.draw.rect(screen, bth_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.draw.rect(screen,(0, 255, 0),(100, 100, 100, 60))
    # """文字"""
    # font = pygame.font.SysFont('Times', 30)
    # title = font.render('clicke',True,(255, 0, 0))
    # screen.blit(title, (120, 120))



    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, 100, 0), (100, 0, 0))
                    pygame.display.update()

            if event.type == pygame.MOUSEBUTTONUP:
                if is_in_rect(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)

相关文章

  • day11 pygame

    鼠标事件

  • Day11 pygame应用

    01.pygame事件 02.鼠标事件的应用 03.鼠标事件的应用2 04.动画效果 05.ballGame 06...

  • day11 pygame应用

    01 pygame事件 02 鼠标事件的运用 03 鼠标事件的应用2 要求:先在屏幕上显示一张图片,鼠标按下移动的...

  • 2018-09-04-pygame

    一、pygame基本操作 import pygame——导入pygame模块 pygame.init()——初始化...

  • Pygame入门--飞机大战案例

    Pygame的快速入门 #导入pygame模块 import pygame #游戏初始化 pygame.init(...

  • Day_10 异常与pygame

    异常捕获 pygame操作流程 pygame显示文字 pygame显示图片与图片操作 pygame基本显示

  • Pygame-hello world

    使用pygame 模块名功能pygame.cdrom访问光驱pygame.cursors加载光标pygame.di...

  • pygame - alphabet

    pygame install pygame install[https://www.pygame.org/wiki...

  • Day12 pygame

    1.pygame基本操作: 1.导入pygame: import pygame.2.初始化:pygame init...

  • Day-18正则表达式2

    pygame游戏基本框架的创建 pygame中图片的显示 字体的显示 图形 Pygame Pygame有很多的模块...

网友评论

      本文标题:day11 pygame

      本文链接:https://www.haomeiwen.com/subject/ksrqvftx.html