Python pygame 主副屏编程时 在副屏上全屏窗口的方法

部署运行你感兴趣的模型镜像

Python在windows环境中编程时,用pygame工具包能够很轻易的完成2D游戏的简单设计,非常好用,相关帖子很多。

而当电脑连接了多块显示器时(注意不是windows的多桌面),系统选择扩展这些显示器后,可以利用的屏幕空间变大,用pygame设计程序窗口的尺寸也可以同步增加,但在设定为全屏时,却无法指定具体的显示器,查找了很多帖子,都未能很好的解决。

最后,经过多次尝试后,发现有可能是因为pygame版本更新的原因,造成参数改变引起的。当前我用的版本是pygame 2.6.0 (SDL 2.28.4, Python 3.11.5),其它版本是否相同未曾试过。

具体办法是指定display函数的display_index =1 ,这个函数的参数为:

screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN | pygame.DOUBLEBUF | pygame.HWSURFACE,depth = 0,display_index = 0,vsync=0)

这里第二给参数列出了一些常用的flag,而关键的是第四个参数,之前找到的帖子中这个参数被前移到的第三个,所以一直试不出来。

完整的测试代码(如果你有两块或更多显示器,并在系统中选择的扩展这些显示器):

import pygame
import sys
import random

# 初始化pygame
pygame.init()

# 获取显示设备数量
num_displays = pygame.display.get_num_displays()
if num_displays < 2:
    print("系统检测到的显示设备数量少于2个。")
    exit(0)

# 设置窗口在第二块显示器上显示
screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN | pygame.DOUBLEBUF | pygame.HWSURFACE,0,1) # 设置窗口在第二块显示器上显示,display_index = 1
#screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN | pygame.DOUBLEBUF | pygame.HWSURFACE,depth = 0,display_index = 0,vsync=0)
pygame.display.set_caption('接苹果小游戏')
info = pygame.display.Info()
screen_width = info.current_w
screen_height = info.current_h
print(f"屏幕宽度:{screen_width},屏幕高度:{screen_height}")

# 设置颜色
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

# 设置苹果和篮子的图片(你需要准备这些图片并放在相应路径下)
apple_image = pygame.image.load('apple.png')
basket_image = pygame.image.load('basket.png')

# 苹果类
class Apple:
    def __init__(self):
        self.pos_x = random.randint(0, screen_width - apple_image.get_width())
        self.pos_y = 0
        self.speed = random.randint(2, 5)

    def move(self):
        self.pos_y += self.speed
        if self.pos_y > screen_height:
            self.__init__()  # 重置苹果位置和速度

    def draw(self):
        screen.blit(apple_image, (self.pos_x, self.pos_y))

# 篮子类
class Basket:
    def __init__(self):
        self.pos_x = screen_width // 2 - basket_image.get_width() // 2
        self.pos_y = screen_height - basket_image.get_height() - 20
        self.speed = 5

    def move_left(self):
        self.pos_x -= self.speed
        if self.pos_x < 0:
            self.pos_x = 0

    def move_right(self):
        self.pos_x += self.speed
        if self.pos_x > screen_width - basket_image.get_width():
            self.pos_x = screen_width - basket_image.get_width()

    def draw(self):
        screen.blit(basket_image, (self.pos_x, self.pos_y))

# 游戏主循环
def game_loop():
    apple = Apple()
    basket = Basket()
    score = 0
    clock = pygame.time.Clock()
    game_over = False

    while not game_over:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    basket.move_left()
                elif event.key == pygame.K_RIGHT:
                    basket.move_right()

        apple.move()
        basket_rect = pygame.Rect(basket.pos_x, basket.pos_y, basket_image.get_width(), basket_image.get_height())
        apple_rect = pygame.Rect(apple.pos_x, apple.pos_y, apple_image.get_width(), apple_image.get_height())

        if basket_rect.colliderect(apple_rect):
            score += 1
            apple.__init__()  # 重置苹果位置和速度

        screen.fill(WHITE)
        apple.draw()
        basket.draw()
        font = pygame.font.Font(None, 50)
        score_text = font.render(f"Score: {score}", 1, GREEN)
        screen.blit(score_text, (20, 20))
        pygame.display.flip()
        clock.tick(30)  # 设置游戏帧率

    pygame.quit()
    sys.exit()

game_loop()

这是一个AI生成的游戏代码,改动的部分就是pygame.display函数的参数设定。祝工作顺利!

您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值