欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

pygame笔记2

程序员文章站 2022-03-16 08:41:55
...

键盘事件控制小球速度

import pygame, sys

pygame.init()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("D:/PYG02-ball.gif") #载入图片
ballrect = ball.get_rect() #生成对应矩形
fps = 300
fclock = pygame.time.Clock() # 计算刷新的时间间隔

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_LEFT:
                speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1) * int(speed[0] / abs(speed[0]))
            elif event.key == pygame.K_RIGHT:
                speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
            elif event.key == pygame.K_UP:
                speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
            elif event.key == pygame.K_DOWN:
                speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1) * int(speed[1] / abs(speed[1]))
    ballrect = ballrect.move(speed[0], speed[1]) # 模拟壁球移动
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = - speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = - speed[1]

    screen.fill(BLACK)
    screen.blit(ball, ballrect) # 让小球附着在矩阵上,随着矩阵移动
    pygame.display.update()
    fclock.tick(fps)

pygame笔记2

pygame笔记2

pygame笔记2

全屏版带esc退出,使用pygame.display.Info 感知屏幕信息

import pygame, sys

pygame.init()
VInfo = pygame.display.Info() # 感知屏幕信息
size = width, height = VInfo.current_w, VInfo.current_h
#size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size, pygame.FULLSCREEN) #每种机制应当设置相对处理方法
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("D:/PYG02-ball.gif") #载入图片
ballrect = ball.get_rect() #生成对应矩形
fps = 300
fclock = pygame.time.Clock() # 计算刷新的时间间隔

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_LEFT:
                speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1) * int(speed[0] / abs(speed[0]))
            elif event.key == pygame.K_RIGHT:
                speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
            elif event.key == pygame.K_UP:
                speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
            elif event.key == pygame.K_DOWN:
                speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1) * int(speed[1] / abs(speed[1]))
            elif event.key == pygame.K_ESCAPE:
                sys.exit()
    ballrect = ballrect.move(speed[0], speed[1]) # 模拟壁球移动
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = - speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = - speed[1]

    screen.fill(BLACK)
    screen.blit(ball, ballrect) # 让小球附着在矩阵上,随着矩阵移动
    pygame.display.update()
    fclock.tick(fps)

pygame笔记2

*伸缩窗体,借助event事件

# import pygame, sys
# 
# pygame.init()
# VInfo = pygame.display.Info() # 感知屏幕信息
# #size = width, height = VInfo.current_w, VInfo.current_h
# size = width, height = 600, 400
# speed = [1, 1]
# BLACK = 0, 0, 0
# screen = pygame.display.set_mode(size, pygame.FULLSCREEN) #每种机制应当设置相对处理方法
# pygame.display.set_caption("Pygame壁球")
# ball = pygame.image.load("D:/PYG02-ball.gif") #载入图片
# ballrect = ball.get_rect() #生成对应矩形
# fps = 300
# fclock = pygame.time.Clock() # 计算刷新的时间间隔
# 
# 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_LEFT:
#                 speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1) * int(speed[0] / abs(speed[0]))
#             elif event.key == pygame.K_RIGHT:
#                 speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
#             elif event.key == pygame.K_UP:
#                 speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
#             elif event.key == pygame.K_DOWN:
#                 speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1) * int(speed[1] / abs(speed[1]))
#             elif event.key == pygame.K_ESCAPE:
#                 sys.exit()
            elif event.type == pygame.VIDEORESIZE:
                size = width, height = event.size[0], event.size[1]
                screen = pygame.display.set_mode(size, pygame.RESIZABLE)
#     ballrect = ballrect.move(speed[0], speed[1]) # 模拟壁球移动
#     if ballrect.left < 0 or ballrect.right > width:
#         speed[0] = - speed[0]
#     if ballrect.top < 0 or ballrect.bottom > height:
#         speed[1] = - speed[1]
# 
#     screen.fill(BLACK)
#     screen.blit(ball, ballrect) # 让小球附着在矩阵上,随着矩阵移动
#     pygame.display.update()
#     fclock.tick(fps)

pygame笔记2

pygame笔记2

设置标题中的图标

pygame笔记2

 

pygame笔记2

窗口最小化时暂停游戏

pygame笔记2

两种刷新方式,flip全刷,update只刷新更新的部分

pygame笔记2

 

相关标签: pygame