Pygame中的程序地形生成是颠倒的

2024-10-02 12:29:56 发布

您现在位置:Python中文网/ 问答频道 /正文

import pygame
import blocks
import random

class World:
    def __init__(self):
        self.tile_list = []
        TILESIZE = 20
        minStoneHeight = 1
        maxStoneHeight = 8
        x = 0
        y = 0
        height = 10
        width = 100


        for x in range(width):
            minHeight = height - 1
            maxHeight = height + 2
            height = random.randrange(minHeight, maxHeight)
            minStoneSpawnDistance = height - minStoneHeight
            maxStoneSpawnDistance = height - maxStoneHeight
            totalStoneSpawnDistance = random.randrange(minStoneHeight, maxStoneHeight)
            for y in range(height):
                if y < totalStoneSpawnDistance:
                    t = blocks.stoneBlock(x*20, y*20 + 100)
                    self.tile_list.append(t)
                else:
                    t = blocks.dirtBlock(x*20, y*20 + 100)
                    self.tile_list.append(t)
            if(totalStoneSpawnDistance == height):
                t = blocks.stoneBlock(x*20, y*20 + 100)
                self.tile_list.append(t)
            else:
                self.tile_list.append(t)
                t = blocks.stoneBlock(x*20, y*20+100)



    def draw(self, screen):
        for tile in self.tile_list:
            tile.draw(screen)

这是我在pygame中用于程序地形生成的代码。我根据C#for Unity的脚本改编了它。问题是pygames网格orgin位于左上角,unitys位于左下角。我想知道如何才能让我的地形不被颠倒


Tags: inimportselfforrandompygamelisttile
1条回答
网友
1楼 · 发布于 2024-10-02 12:29:56

这是个问题。您必须在应用程序循环而不是事件循环中移动播放器:

running = True
while running:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                currentPlayerImg = playerImgBack
                playerYSpeed = -5

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                playerYSpeed = 0

    #< | INDENTATION
    
    playerX += playerXSpeed
    playerY += playerYSpeed

    screen.blit(bgImg, (0, 0))
    screen.blit(currentPlayerImg, (playerX, playerY))
    pygame.display.update()

相关问题 更多 >

    热门问题