首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何移动游戏中网格的位置

如何移动游戏中网格的位置
EN

Stack Overflow用户
提问于 2020-07-06 07:37:26
回答 1查看 426关注 0票数 1
代码语言:javascript
复制
import pygame

pygame.init()

width = 800
height = 600
running = True
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Dijkstra's Path-Finding Algorithm Solver")
icon = pygame.image.load('icon.jpg')
pygame.display.set_icon(icon)

def title():
    button_font = pygame.font.Font('TrajanPro-Regular.otf', 40)
    rect_display = button_font.render('Dijkstra Path-Finding Algorithm', True, (255, 255, 255))
    # display global total deaths
    screen.blit(rect_display, (12, 10))

def title_underline():
    # create the button
    rect = pygame.Rect(0, 60, 800, 3)
    rect_display = pygame.draw.rect(screen, [255, 255, 255], rect)
    button_font = pygame.font.Font('TrajanPro-Regular.otf', 100)
    rect_display = button_font.render('', True, (255, 255, 255))
    # display global total deaths
    screen.blit(rect_display, (270, 198))

def grid():
    blockSize = 20 #Set the size of the grid block
    for x in range(width):
        for y in range(height):
            rect = pygame.Rect(x*blockSize, y*blockSize, blockSize, blockSize)
            pygame.draw.rect(screen, (200,200,200), rect, 1)

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.fill((0, 0, 0))
    title()
    title_underline()
    grid()
    pygame.display.update()

我已经为我的项目创建了一个标题,我创建了一个网格,我想运行我的应用程序,但是网格是在我的屏幕上的每一个x,y位置创建的。但我希望网格只在标题下,而不是在上面。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-06 07:45:19

定义网格的左上角坐标(grid_xgrid_y),并在构造单元格的矩形时添加坐标。例如:

代码语言:javascript
复制
def grid():

    grid_x = 12
    grid_y = 30
 
    blockSize = 20 #Set the size of the grid block
    for x in range(width):
        for y in range(height):
            
            rect = pygame.Rect(
                grid_x + x*blockSize, grid_y + y*blockSize,
                blockSize, blockSize)
           
            pygame.draw.rect(screen, (200,200,200), rect, 1)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62751326

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档