gpt4 book ai didi

python - Pygame - 将表面区域的滚动锁定到有限的数量

转载 作者:行者123 更新时间:2023-12-01 09:17:22 24 4
gpt4 key购买 nike

我正在玩 pygame,我正在制作一个内存游戏。在我的内存游戏中,我需要在屏幕中底部有一个小菜单,在屏幕顶部有玩家列表。游戏管理器将在屏幕左上角输出消息。

放置卡片的可玩棋盘应位于屏幕中央,左右留有小边距,距顶部和底部约 200 像素。(例如,如果屏幕宽度和高度为 1200 x 800,可玩棋盘的宽度应为 2 至 1998,高度为 200 至 600。

我已经制作了我的想法的屏幕截图:

Screenshot.

我已经提出了带有卡片的可滚动板的想法,尽管现在它是硬编码的,但我主要需要这个想法而不是代码,因为我很难弄清楚它应该如何工作。

可玩的棋盘(屏幕截图中的游戏棋盘)应该通过键盘移动(向上、向右、向下、向左箭头)。我想要做的事情是允许每列超过 4 张、每行超过 12 张卡片,这样当卡片数量超过 12 x 4 时,游戏板可以移动,但不会退出标记的红色方 block 在屏幕截图上(所以它是一个可滚动的表面)。我想将可滚动区域的宽度限制为 1200 像素,高度为 400 px,但该区域只能在红色区域内滚动。

如果这不起作用或有其他计划,是否有任何方法可以以不同的方式记录点击次数?假设当我以菜单区域位于游戏板上方并且单击菜单的方式移动按键时,我不希望也单击菜单下的卡。有什么办法可以锁定菜单下的内容吗?

添加以下代码示例:

from pygame import *
import sys

init()
screen_width = 1200
screen_height = 800
screen = display.set_mode((screen_width, screen_height))


board_width = 2000
board_height = 1000
playable_board = Surface((board_width, board_height))
playable_board = playable_board.convert()

screen.fill((255, 255, 255))


#draw.rect(playable_board, (125, 125, 125), (2, 200, board_width, board_height))
for x in range(0, 50):
draw.rect(playable_board, (100, 100, 100), (x * 100 + 5, 200, 90, 90))
draw.rect(playable_board, (100, 100, 100), (x * 100 + 5, 310, 90, 90))
draw.rect(playable_board, (100, 100, 100), (x * 100 + 5, 420, 90, 90))
draw.rect(playable_board, (100, 100, 100), (x * 100 + 5, 530, 90, 90))
# draw.rect(playable_board, (100, 100, 100), (x * 100 + 5, 640, 90, 90))

point_x = 0
point_y = 0
point_change = -30

animationTimer = time.Clock()
endProgram = False


while not endProgram:
for e in event.get():
pass
key_pressed = key.get_pressed()
if e.type == QUIT:
endProgram = True
if e.type == MOUSEBUTTONUP:
mouse_x, mouse_y = e.pos
display.set_caption(('Coordinates: ' + str(mouse_x + abs(point_x)) + 'x' + str(mouse_y + abs(point_y))))
draw.rect(playable_board, (50, 200, 50), (mouse_x + abs(point_x), mouse_y + abs(point_y), 10, 10))
if key_pressed[K_LEFT]:
point_x -= point_change
if key_pressed[K_RIGHT]:
point_x += point_change
if key_pressed[K_UP]:
point_y -= point_change
if key_pressed[K_DOWN]:
point_y += point_change
if point_x > 0:
point_x = 0
if point_x < -(board_width - screen_width):
point_x = -(board_width - screen_width)
if point_y > 0:
point_y = 0
if point_y < -(board_height - screen_height):
point_y = -(board_height - screen_height)
screen.blit(playable_board, (point_x, point_y, screen_width, screen_height))
draw.rect(screen, (100, 255, 100), (500, 650, 200, 150))
draw.rect(screen, (100, 255, 100), (100, 0, 1000, 50))
draw.rect(screen, (100, 255, 100), (0, 70, 250, 100))
animationTimer.tick(60)
display.update()

最佳答案

我会创建一个 subsurface playable_board 的,然后将其 blit 到所需的坐标。您需要定义一个具有所需地下尺寸的矩形(此处称为“区域”),并通过增加 xy 属性来滚动它。要将 area 矩形保留在 board_rect 内(以防止 ValueError),您可以调用 pygame.Rect.clampclamp_ip 方法。

import pygame as pg

pg.init()
screen = pg.display.set_mode((1200, 800))

board = pg.Surface((2000, 1000))
board.fill((20, 70, 110))
board_rect = board.get_rect()

# Append the rects to a list if you want to use
# them for collision detection.
rects = []
for y in range(11):
for x in range(22):
rect = pg.draw.rect(board, (100, 100, 100), (x*95, y*95, 90, 90))
rects.append(rect)

# The blit position of the subsurface.
pos = (20, 200)
point_change = -10
# This rect is needed to create a subsurface with the size (1160, 400).
area = pg.Rect(0, 0, 1160, 400)
# Use the area rect to create a subsurface of the board.
board_subsurface = board.subsurface(area)

clock = pg.time.Clock()
endProgram = False

while not endProgram:
for e in pg.event.get():
if e.type == pg.QUIT:
endProgram = True

key_pressed = pg.key.get_pressed()
# Scroll by changing the x and y coordinates of the area rect.
if key_pressed[pg.K_LEFT]:
area.x += point_change
elif key_pressed[pg.K_RIGHT]:
area.x -= point_change
if key_pressed[pg.K_UP]:
area.y += point_change
elif key_pressed[pg.K_DOWN]:
area.y -= point_change
# Check if one of the movement keys was pressed.
if any(key_pressed[key] for key in (pg.K_LEFT, pg.K_RIGHT, pg.K_UP, pg.K_DOWN)):
# Clamp the area rect to the board_rect, otherwise calling
# `board.subsurface` could raise a ValueError.
area.clamp_ip(board_rect)
# Create a new subsurface with the size of the area rect.
board_subsurface = board.subsurface(area)

screen.fill((30, 30, 30))
screen.blit(board_subsurface, pos)
pg.draw.rect(screen, (100, 255, 100), (500, 650, 200, 150))
pg.draw.rect(screen, (100, 255, 100), (100, 0, 1000, 50))
pg.draw.rect(screen, (100, 255, 100), (0, 70, 250, 100))
clock.tick(60)
pg.display.update()

关于python - Pygame - 将表面区域的滚动锁定到有限的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51118939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com