马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
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 pygameimport sysimport random# 初始化pygamepygame.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_wscreen_height = info.current_hprint(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函数的参数设定。祝工作顺遂!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |