python之pygame(不定时更新中)

目录

pygame.dispaly, pygame.event, pygame.draw:

pygame.dispaly:

pygame有且仅有一个屏幕;左上角坐标(0,0);以像素为单位。

#屏幕尺寸和模

pygame.display.set_mode(r = (0,0), flag = 0)
#r是游戏屏幕分辨率,以元组形式输入(weight, height)
#flag用来控制显示类型,可用 | 组合使用,常用标签有:
pygame.RESIZABLE    #窗口大小可调
pygame.NOFRAME      #窗口没有边界显示
pygame.FULLSCREEN   #窗口全屏显示
(注意每种显示要配合相应的处理机制)

vinfo = pygame.display.Info()
#产生一个显示信息对象VideoInfo,表达当前屏幕参数信息
vinfo.current_w    #当前显示模式或窗口的像素宽度
vinfo.current_h    #当前显示模式或窗口的像素高度

pygame.VIDEORESIZE
#这是一种窗口大小更改的事件
#事件发生后,返回event.size元组,包含新窗口的宽度和高度
.size[0]    #宽度,也可以用event.w
.size[1]    #高度,也可以用event.h
#返回参数仅在事件发生时有用
#example
if event.type == pygame.VIDEORESIZE:
      size = width, height = event.size[0], event.size[1]
      screen = pygame.display.set_mode(size, pygame.RESIZABLE)

#窗口标题和图标

pygame.display.set_caption(title, icontitle = None)
#title设置窗口的标题内容
#icontitle设置图表化后的小标题,小标题可选,部分系统没有

pygame.display.set_icon(surface)
#设置窗口的图标效果
#图标是一个Surface对象

pygame.display.get_caption()
#返回当前设置窗口的标题和小标题内容,(title, icontitle)

#窗口感知和刷新

pygame.display.get_active()
#当窗口在系统中显示(屏幕绘制/非图标化)时返回True,否则返回False
#可以用来判断游戏窗口是否被最小化

pygame.display.flip()
#重新绘制整个窗口

pygame.display.update()
#仅重新绘制窗口中有变化的区域,相比.flip()执行更快

 

pygame.event:

#键盘事件

pygame.event.KEYDOWN    #键盘按下事件
pygame.event.KEYUP      #键盘释放事件
#属性
event.key    #按键的常量名称
event.mod    #按键修饰符的组合值

event.mod = KMOD_ALT|KMOD_SHIFT    #修饰符的按位或运算

#鼠标事件

#鼠标移动事件
pygame.event.MOUSEMOTION        
#属性
event.pos    #鼠标当前坐标值(x,y),相对于窗口左上角
event.rel    #鼠标相对运动距离(x,y),相对于上次事件
event.rel    #鼠标按钮状态(a,b,c),对应于鼠标的三个键(左 中 右)

pygame.event.MOUSEBUTTONUP      #鼠标释放事件
pygame.event.MOUSEBUTTONDOWN    #鼠标按下事件
#属性
event.pos    #鼠标当前坐标值(x,y)
event.button #鼠标按下键编号,左 中 右对应1 2 3

#example

#鼠标,键盘事件处理
import pygame, sys
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Pygame事件处理")
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.unicode == "":
                print("[KEYDOWN]:", "#", event.key, event.mod)
            else:
                print("[KEYDOWN]:", event.unicode, event.key, event.mod)
        elif event.type == pygame.MOUSEMOTION:
            print("[MOUSEMOTION]:", event.pos, event.rel, event.buttons)
        elif event.type == pygame.MOUSEBUTTONUP:
            print("[MOUSEBUTTONUP]:", event.pos, event.button)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            print("[MOUSEBUTTONDOWN]:", event.pos, event.button)
    pygame.display.update()
​

pygame.event.get()

从事件列表中获得事件列表

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

可以增加参数,获得某类或某些类事件:

pygame.event.get(type)
pygame.event.get(typelist)

pygame.event.poll()

从事件队列中获得一个事件,事件将从事件队列中删除,如果事件队列为空,则返回event.NOEVENT

while True:
    event = pygame.event.poll()

pygame.event.clear()

从事件队列中删除事件,默认删除所有事件,可以增加参数,删除某类或某些类事件

pygame.event.clear(type)
pygame.event.clear(typelist)

pygame.draw:

官方文档

pygame库用来绘制形状的类

参数列表中的Surface是当前绘制屏幕的名称,color是RGB色彩模式下的颜色,例如 黑色(0, 0, 0),白色(255, 255, 255),width = 0是绘制形状的边的宽度,如果不传入的话width=0默认为填充

pygame.draw.rect(Surface, color, Rect, width=0)

绘制一个矩形

参数Rect是矩形参数,格式为[x, y, width, height]

examples for pygame.draw.rect

pygame.draw.circle(Surface, color, pos, radius, width=0)

以某点为圆心绘制一个圆形

参数pos是圆心的位置,radius是半径大小

examples for pygame.draw.circle

pygame.draw.ellipse(Surface, color, Rect, width=0)

绘制一个椭圆

通过给出矩形的参数,从而绘制一个内切于矩形的椭圆

examples fo pygame.draw.ellipse

pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1)

绘制椭圆的一部分

两个角度分别是起始和结束角度,最右边为0度

examples for pygame.draw.arc

pygame.draw.line(Surface, color, start_pos, end_pos, width=1)

绘制一条直线

参数两个pos分别是起始位置和结束位置

examples for pygame.draw.line

pygame.draw.lines(Surface, color, closed, pointlist, width=1)

绘制多条连续直线

参数closed为True时首尾相连构成封闭,pointlist为顶点坐标

examples fo pygame.draw.lines

A big example for this module

# Import a library of functions called 'pygame'
import pygame
from math import pi

# Initialize the game engine
pygame.init()

# Define the colors we will use in RGB format
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

# Set the height and width of the screen
size = [400, 300]
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Example code for the draw module")

# Loop until the user clicks the close button.
done = False
clock = pygame.time.Clock()

while not done:

    # This limits the while loop to a max of 10 times per second.
    # Leave this out and we will use all CPU we can.
    clock.tick(10)

    for event in pygame.event.get():  # User did something
        if event.type == pygame.QUIT:  # If user clicked close
            done = True  # Flag that we are done so we exit this loop

    # All drawing code happens after the for loop and but
    # inside the main while done==False loop.

    # Clear the screen and set the screen background
    screen.fill(WHITE)

    # Draw on the screen a GREEN line from (0,0) to (50.75) 
    # 5 pixels wide.
    pygame.draw.line(screen, GREEN, [0, 0], [50, 30], 5)

    # Draw on the screen a GREEN line from (0,0) to (50.75) 
    # 5 pixels wide.
    pygame.draw.lines(screen, BLACK, False, [[0, 80], [50, 90], [200, 80], [220, 30]], 5)

    # Draw on the screen a GREEN line from (0,0) to (50.75) 
    # 5 pixels wide.
    pygame.draw.aaline(screen, GREEN, [0, 50], [50, 80], True)

    # Draw a rectangle outline
    pygame.draw.rect(screen, BLACK, [75, 10, 50, 20], 2)

    # Draw a solid rectangle
    pygame.draw.rect(screen, BLACK, [150, 10, 50, 20])

    # Draw an ellipse outline, using a rectangle as the outside boundaries
    pygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2)

    # Draw an solid ellipse, using a rectangle as the outside boundaries
    pygame.draw.ellipse(screen, RED, [300, 10, 50, 20])

    # This draws a triangle using the polygon command
    pygame.draw.polygon(screen, BLACK, [[100, 100], [0, 200], [200, 200]], 5)

    # Draw an arc as part of an ellipse. 
    # Use radians to determine what angle to draw.
    pygame.draw.arc(screen, BLACK, [210, 75, 150, 125], 0, pi / 2, 2)
    pygame.draw.arc(screen, GREEN, [210, 75, 150, 125], pi / 2, pi, 2)
    pygame.draw.arc(screen, BLUE, [210, 75, 150, 125], pi, 3 * pi / 2, 2)
    pygame.draw.arc(screen, RED, [210, 75, 150, 125], 3 * pi / 2, 2 * pi, 2)

    # Draw a circle
    pygame.draw.circle(screen, BLUE, [60, 250], 40)

    # Go ahead and update the screen with what we've drawn.
    # This MUST happen after all the other drawing commands.
    pygame.display.flip()

# Be IDLE friendly
pygame.quit()

 

你可能感兴趣的:(python)