day 11

01 pygame 事件

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 :
    # MOUSEBUTTONUP :
    # MOUSEMOTION :

    #KEYDOWN:
    # KEYUP:
    while True:
        for event in pygame.event.get():
            # 不同的类型对应不同的type值不一样


            if event.type == pygame.QUIT:
                exit()

if __name__ == "__main__":
    pass

02 pygame 鼠标事件

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

03 py game键盘事件


    is_move = False


    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            # 鼠标按下 图片移动
            if event.type == pygame.MOUSEBUTTONDOWN:
                w , h = image.get_size()
                if is_in_rect(event.pos,(image_x,image_y,w,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))
                    x , y = event.pos
                    image_w ,image_h =image.get_size()

                    # 保证鼠标的图片中心
                    image_y =y -image_h/2
                    image_x = x-image_w/2
                    screen.blit(image,(x -image_w/2,y -image_h/2))
                    pygame.display.update()

04 动画效果

# 原理 : 不断刷新界面上的内容

import pygame
from random import randint

    # 静态文字
def static_page(screen):
    font = pygame.font.SysFont("Times",40)
    title =font.render("welcome",True,(0,0,0))
    screen.blit(title,(100,100))
    # 动态文字
def animatiaon_title(screen):
    font = pygame.font.SysFont("times", 40)
    title = font.render("123456", True, (randint(0,255),randint(0,255),randint(0,255)))
    screen.blit(title, (200, 200))



if __name__ == "__main__":
    pygame.init( )
    screen = pygame.display.set_mode((600,400))
    screen.fill((255,255,255))


    pygame.display.flip()




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

        # 在下面写每一帧的变化
        screen .fill((255,255,255))
        static_page(screen)
        animatiaon_title(screen)
        pygame.display.update()

05 多球效果

import pygame    # 导入pygame包
import random
random_color = random.randint(0,255),random.randint(0,255),random.randint(0,255)


if __name__ == "__main__":

   pygame.init()
   screen = pygame.display.set_mode((600,600))
   screen.fill((255,255,255))
   screen = pygame.display.set_caption("大球吃小球")
   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(10, 25),
                   'pos': event.pos,
                   'color': random_color,
                   'x_speed': random.randint(-3, 3),
                   'y_speed': random.randint(-3, 3)
               }
               all_balls.append(ball)

       screen.fill((255, 255, 255))
       for ball_dict in all_balls:
           x, y = ball_dict["pos"]
           x_speed = ball_dict["x_speed"]
           y_speed = ball_dict["y_speed"]
           x += x_speed
           y += y_speed
           pygame.draw.circle(screen, ball_dict['color'], (x, y), ball_dict['r'])
           ball_dict['pos'] = x, y

       pygame.display.update()

你可能感兴趣的:(day 11)