Day_11 pygame

pygame事件,鼠标事件与键盘事件
        for event in pygame.event.get():
            # 不同类型的事件对应的type值不一样
            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)
            # 鼠标事件产生位置 event,pos获取鼠标产生位置


            #键盘相关事件
            # 键盘产生位置 event.key 这是以ASCII编码形式返回的位置

            if event.type == pygame.KEYUP:
                print('键盘松开事件',chr(event.key))
            if event.type == pygame.KEYDOWN:
                print('键盘按下事件',chr(event.key))


鼠标事件的应用
import pygame
import random
def rand_color():
    return random.randint(0,255)
def draw_ball(screen):
    index = random.randint(0,600)
    indey = random.randint(0,600)
    if 480<=index<=600 and 480<=indey<=580:
        pass
    else:
        pygame.draw.circle(screen, (rand_color(), rand_color(), rand_color()), (index,indey), random.randint(10, 20))

        pygame.display.update()
def draw_button():
    # 画按钮 坐标(500,500) 大小 30*40
    pygame.draw.rect(screen,(189,240,32),(500,500,80,60))
    # 按钮文字
    font = pygame.font.SysFont('Times',30)
    title = font.render('click',True,(145,34,64))
    screen.blit(title,(515,515))
def is_in_rect(rect,point):
    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((600,600))
    screen.fill((255,255,255))
    pygame.display.set_caption('鼠标事件')

    draw_button()
    pygame.display.flip()



    while True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

            if event.type == pygame.MOUSEBUTTONDOWN:
                if is_in_rect((500,500,80,60),event.pos):
                    draw_ball(screen)


image.png

结果:点击click随机生成一个球 click按钮内不产生球

鼠标移动事件
import pygame

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600,600))
    screen.fill((123,123,20))
    pygame.display.set_caption('图片事件')

    def is_in_rect(pos,rect):
        x,y = event.pos
        rx,ry,rw,rh = rect
        if (rx<=x

结果:图片随鼠标运动


image.png
动画效果
import pygame
import random
def rand_color():
    return random.randint(0,255)
def static_page(screen):
    font = pygame.font.SysFont('Times',40)
    title = font.render('hello',True,(233,233,0))
    screen.blit(title,(280,260))
def animation_title(screen):
    font = pygame.font.SysFont('Times', 40)
    title = font.render('hello', True, (rand_color(),rand_color(),rand_color()))
    screen.blit(title, (200, 200))
if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600,600))
    screen.fill((123,123,30))
    static_page(screen)
    pygame.display.flip()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
        # 每一帧要显示的内容
        pygame.time.wait(50)
        pygame.time.delay(50)
        # 阻塞线程
        # 动画前要将原来的内容全部清空
        screen.fill((123,123,20))
        static_page(screen)
        animation_title(screen)
        # 更新显示
        pygame.display.update()


结果:变化文字颜色


image.png
鼠标点击生成小球
"""__author__== God"""

import pygame
import random
def draw_ball(place,color,pos,radius):
    pygame.draw.circle(place,color,pos,radius)
if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600,600))
    screen.fill((123,123,123))
    pygame.display.flip()
    ball_x = 100
    ball_y = 100
    # x_speed = random.randint(1,8)
    # y_speed = random.randint(1,8)
    x_speed = 0
    y_speed = 0
    # 方向对应的值
    Up = 273
    Down = 274
    Left =276
    Right =275
    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 = -20
                    x_speed = 0
                elif event.key == Down:
                    y_speed = 20
                    x_speed = 0
                elif event.key == Right:
                    x_speed = 20
                    y_speed = 0
                elif event.key == Left:
                    x_speed = -20
                    y_speed = 0
        screen.fill((123,123,200))
        pygame.time.delay(50)
        ball_x+=x_speed
        ball_y+=y_speed
        if ball_x+10>=600 or ball_x-10<=0:
            x_speed*=-1
        if ball_y+10>=600 or ball_y-10<=0:
            y_speed*=-1

            # y_speed*=-1

        draw_ball(screen,(123,123,1),(ball_x,ball_y),20)
        pygame.display.update()

结果:键盘控制运动小球方向

image.png
大球吃小球
"""__author__== God"""
import pygame
import random
from math import sqrt
def rand_color():
    return  random.randint(0,255),random.randint(0,255),random.randint(0,255)

def crash(item1,item2):
    item1x,item1y = item1['pos']
    item2x,item2y = item2['pos']
    dx = item2x- item1x
    dy = item2y - item1y
    dist = int(sqrt(dx*dx+dy*dy))
    if distitem1['r']:
            item2['r']+=1
            if item1 in all_balls[:]:
                all_balls.remove(item1)
        else:
            item1['r']+=1
            if item2 in all_balls[:]:
                all_balls.remove(item2)

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600,600))
    screen.fill((123,123,20))
    pygame.display.flip()
    all_balls = []



    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            if event.type == pygame.MOUSEBUTTONDOWN:

                ball ={
                    'r':random.randint(5,15),
                    'color':rand_color(),
                    'pos':event.pos,
                    'x_speed':random.randint(-3,3),
                    'y_speed':random.randint(-3,3)
                }
                all_balls.append(ball)
        screen.fill((123, 123, 20))
        for ball in all_balls:

            # pygame.time.wait(2)
            x,y = ball['pos']
            x+=ball['x_speed']
            y+=ball['y_speed']
            if x + ball['r'] >= 600 or x -ball['r'] <= 0:
                ball['x_speed'] *= -1
            if y + ball['r'] >= 600 or y - ball['r'] <= 0:
                ball['y_speed']*= -1

            ball['pos'] = x,y
            pygame.draw.circle(screen,ball['color'],(x,y),ball['r'])
        pygame.display.update()
        index = len(all_balls)
        for item1 in all_balls:
            for item2 in all_balls:
                if item1!=item2:
                    crash(item1,item2)




结果:大球吃小球

image.png

你可能感兴趣的:(Day_11 pygame)