pygame基础操作练习

pygame基础操作练习

import pygame
import sys
import pygame.freetype

pygame.init()#对pygame的内容初始化
fclock = pygame.time.Clock()#创造一个Clock对象,用于操作时间
vinfo = pygame.display.Info()#获取屏幕信息大小尺寸

black = 0,0,0#窗口背景颜色
bgcolor = pygame.Color('black')#创造一个保存颜色的对象,初始化颜色为黑色

speed = [1,1]#速度
fps = 300 #每秒的帧率参数
still = False

ball = pygame.image.load('image/Aline1.png')
ball_rect = ball.get_rect()
icon = pygame.image.load('image/Aline1.png')

#screen_rect = screen_width,screen_height = vinfo.current_w,vinfo.current_h#窗口的宽高设置为全屏大小
screen_rect = screen_width,screen_height = 300,400#窗口的宽高


#pygame.RESIZABLE|pygame.NOFRAME|pygame.FULLSCREEN
#print(pygame.display.Info())#获取窗口信息,没定义窗口时返回屏幕信息
screen = pygame.display.set_mode(screen_rect,pygame.RESIZABLE)#窗口大小,返回一个窗口对象

pygame.display.set_caption("计算机博弈大赛")#窗口标题
pygame.display.set_icon(icon)#设置窗口图标

def RGBChannel(dat):
    return 0 if dat < 0 else(255 if dat > 255 else int(dat))
while True:
    for event in pygame.event.get():#获取事件
        if event.type == pygame.QUIT:
            sys.exit()

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN:
                speed[1] = speed[1] + 2
            elif event.key == pygame.K_UP:
                speed[1] = speed[1] - 2
            elif event.key == pygame.K_LEFT:
                speed[0] = speed[0] -2
            elif event.key == pygame.K_RIGHT:
                speed[0] = speed[0] + 2
            elif event.key == pygame.K_SPACE:
                sys.exit()

        elif event.type == pygame.VIDEORESIZE:#动态改变屏幕大小
            screen_rect = screen_width,screen_height = event.size[0],event.size[1]
            pygame.display.set_mode(screen_rect,pygame.RESIZABLE)

        elif event.type == pygame.MOUSEBUTTONDOWN:#按下鼠标
            if event.button == 1:
                still = True
        elif event.type == pygame.MOUSEBUTTONUP:#松开鼠标
            if event.button == 1:
                still = False
                ball_rect = ball_rect.move(event.pos[0] - ball_rect.left,event.pos[1] - ball_rect.top)
        elif event.type == pygame.MOUSEMOTION:#鼠标移动
            if event.buttons[0] == 1:
                ball_rect = ball_rect.move(event.pos[0] - ball_rect.left, event.pos[1] - ball_rect.top)

    if pygame.display.get_active() and not still :#判断屏幕是否被最小化,没有最小化返回true
        ball_rect = ball_rect.move(speed[0], speed[1])  # 改变象征着图片的矩形的位置

    if ball_rect.right > screen_width or ball_rect.left < 0:
        speed[0] = -speed[0]
    if ball_rect.top < 0 or ball_rect.bottom > screen_height:
        speed[1] = -speed[1]

    bgcolor.r = RGBChannel(ball_rect.right%255)
    bgcolor.g = RGBChannel(ball_rect.left%255)
    bgcolor.b = RGBChannel(min(speed[0],speed[1])*255/max(speed[1],speed[0],1))

    screen.fill(bgcolor)#用背景色覆盖窗口
    screen.blit(ball,ball_rect)#将图片的显示位置改变到象征着图片的矩形的位置上

    pygame.display.update()#更新屏幕
    fclock.tick(fps)#每秒更新的帧数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值