Pygame 如何创建一个空的窗口

Pygame 如何创建一个空的窗口

Pygame

Pygame 是一个用于开发游戏和多媒体应用程序的Python库。它提供了处理用户输入、图形、声音和其他多媒体相关任务的功能。

Pygame 是建立在 SDL(Simple DirectMedia Layer) 库上的,提供对音频、键盘、鼠标、游戏手柄和图形硬件的底层访问。Pygame提供了对SDL的高级接口,使得Python开发者更容易使用。

使用Pygame,开发者可以创建2D游戏、街机风格游戏、教育游戏、模拟器和其他多媒体应用程序。Pygame提供了一系列模块来处理图形、音频、输入和其他与游戏相关的任务。它还包括用于处理图像和声音文件、构建游戏菜单和界面的工具。

Pygame 是开源的,并且在 LGPL(Lesser General Public License)GPL(General Public License) 下可用。可以使用Python包管理器pip安装它,也可以直接从Pygame网站下载。

安装Pygame

按照以下步骤安装Pygame

  • 第1步 - 确保您的系统上已安装Python。Pygame适用于3.5及以上的Python版本。您可以从官方网站下载Python(https://www.python.org/downloads/)。

  • 第2步 - 打开终端/命令提示符窗口。

  • 第3步 - 使用pip安装Pygame,输入以下命令并按Enter键 –

pip install pygame
  • 第4步 − 等待安装完成。根据你的网络速度,可能需要几分钟时间。

  • 第5步 − 通过在代码中导入pygame进行测试。如果没有看到任何错误消息,则表示pygame已成功安装在你的系统上。

注意 − 根据你的操作系统,你可能需要安装附加的库或软件包才能使Pygame正常工作。查阅Pygame文档以获取有关系统特定要求的更多信息。

PyGame 窗口

语法

用户可以按照以下语法创建一个空的PyGame窗口。

import pygame
pygame.init()
size = (800, 600)  # Width, Height
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Pygame Window")

创建一个空的Pygame窗口,你可以按照以下步骤进行:

  • 导入pygame后,通过调用pygame.init()函数进行初始化。该函数初始化所有的Pygame模块,应该在其他任何Pygame函数之前调用。

  • 通过创建一个包含像素宽度和高度的元组来设置窗口的大小。

  • 通过调用pygame.display.set_mode()函数并将大小元组作为参数传入来创建Pygame窗口。该函数会创建一个指定大小的新Pygame窗口。

  • 通过调用pygame.display.set_caption()函数来设置窗口的标题。该函数将窗口的标题设置为指定的字符串。

  • 游戏循环:最后,你需要设置一个游戏循环来保持窗口的打开。

  • 游戏循环应包含以下代码。该循环无限运行,直到用户通过点击关闭按钮或按下 Alt+F4 来关闭窗口。

运行这段代码时,你应该看到一个标题为“我的Pygame窗口”的空白Pygame窗口。

示例1

这个程序创建一个Pygame窗口,并包含一个处理事件、使用Pygame内置绘图函数绘制圆形、矩形和线条,以及更新窗口的主游戏循环。你可以修改 pygame.draw.circle()、pygame.draw.rect()pygame.draw.line() 函数的参数来改变形状的位置、大小、颜色和线条的粗细。

import pygame
pygame.init()

window_size = (800, 600)
window = pygame.display.set_mode(window_size)
pygame.display.set_caption("My Pygame Window")

running = True
while running:
   # Handle events
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         running = False

   # Draw shapes
   window.fill((255, 255, 255))
   pygame.draw.circle(window, (255, 0, 0), (150, 200), 50)
   pygame.draw.rect(window, (0, 200, 0), (100, 300, 300, 200))
   pygame.draw.line(window, (0, 0, 100), (100, 100), (700, 500), 5)

   pygame.display.flip()
pygame.quit()

输出

Pygame 如何创建一个空的窗口

示例2

此程序创建了一个Pygame窗口,并添加了一个带有白色矩形形状、黑色文本标签和悬停效果的按钮,当鼠标光标悬停在按钮区域时,按钮颜色会发生变化。程序还处理鼠标事件,以检测按钮被点击并将消息打印到控制台。您可以通过修改button_rect、button_font和button_text对象的属性来更改按钮的位置、大小、颜色、字体和标签。还显示一个时钟,显示程序启动以来的经过时间(以秒为单位)。

该程序还使用Pygame的Clock对象将帧率限制为60 FPS。您可以通过修改clock_font和clock_text_rect对象的属性来更改时钟文本的字体大小、颜色和位置。

import pygame
pygame.init()

window_size = (800, 600)
window = pygame.display.set_mode(window_size)
pygame.display.set_caption("My Pygame Window")

# Define button properties
button_color = (255, 255, 255)
button_hover_color = (200, 200, 200)
button_rect = pygame.Rect(300, 250, 200, 100)
button_font = pygame.font.SysFont(None, 36)
button_text = button_font.render("Click me!", True, (0, 0, 0))
button_text_rect = button_text.get_rect(center=button_rect.center)

# Initialize Pygame clock
clock = pygame.time.Clock()

running = True
while running:
   # Limit the frame rate to 60 FPS
   clock.tick(60)

   # Handle events
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         running = False

      # Handle mouse events
      if event.type == pygame.MOUSEBUTTONDOWN:
         mouse_pos = pygame.mouse.get_pos()
         if button_rect.collidepoint(mouse_pos):
            print("Button clicked!")

      if event.type == pygame.MOUSEMOTION:
         mouse_pos = pygame.mouse.get_pos()
         if button_rect.collidepoint(mouse_pos):
            button_color = button_hover_color
         else:
            button_color = (255, 255, 255)

   # Draw button and clock
   window.fill((128, 128, 128))
   pygame.draw.rect(window, button_color, button_rect)
   window.blit(button_text, button_text_rect)
   current_time = pygame.time.get_ticks()
   clock_text = button_font.render(f"Time: {current_time/1000:.2f} s", True, (255, 255, 255))
   clock_text_rect = clock_text.get_rect(topright=(780, 10))
   window.blit(clock_text, clock_text_rect)

   pygame.display.flip()
pygame.quit()

输出

Pygame 如何创建一个空的窗口

结论

在本教程中,我们学习了关于PyGame的知识,如何安装PyGame,并且使用代码创建了一个空的PyGame窗口。我们学习了如何在example2中绘制形状,

并且我们还学习了如何在example3中添加按钮和时钟。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程