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()

 

  • 8
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Pygame是一个基于Python的游戏开发库,它提供了一系列用于游戏开发的功能和工具。通过Pygame,你可以使用Python语言来创建各种类型的游戏,包括坦克大战。 在Pygame中开发坦克大战游戏,你可以按照以下步骤进行: 1. 安装Pygame库:首先,你需要在你的Python环境中安装Pygame库。可以使用pip命令来安装,如下所示: ``` pip install pygame ``` 2. 导入Pygame库:在你的Python代码中,使用`import pygame`语句来导入Pygame库。 3. 初始化Pygame:在你的代码中,使用`pygame.init()`函数来初始化Pygame。 4. 创建游戏窗口:使用`pygame.display.set_mode()`函数创建一个游戏窗口,设置窗口的大小和其他属性。 5. 加载游戏资源:加载游戏所需的图像、音频等资源文件,并将其存储在适当的变量中。 6. 处理用户输入:使用Pygame提供的事件处理机制来处理用户的键盘或鼠标输入。 7. 更新游戏状态:根据用户输入和游戏规则,更新游戏中的各个元素的状态。 8. 绘制游戏画面:使用Pygame提供的绘图函数来绘制游戏中的各个元素,包括坦克、子弹、地图等。 9. 游戏循环:使用一个无限循环来不断更新游戏状态和绘制游戏画面,以实现游戏的动态效果。 10. 游戏结束:根据游戏规则,判断游戏是否结束,并在适当的时候退出游戏循环。 以上是一个简单的坦克大战游戏开发的基本步骤。当然,具体的实现还需要根据你的需求和设计来进行。你可以参考Pygame官方文档和示例代码,以及其他相关教程和资源来更详细地学习和了解Pygame的使用。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值