如何制作墙图?

时间:2019-04-26 23:25:29

标签: python pygame

我正在制作游戏,但我想通过增加墙壁来使游戏更难。我发现它叫做tilemap。但这是多种颜色,我只想要一种颜色,灰色。我希望它像1,1,1,1, 1,0,0,1, 1,0,0,1, 1,1,1,1

0 =无。 1 =灰色矩形。我还需要像WIDTH = 16 HEIGHT = 9 TILESIZE = dw/WIDTH

但是我找不到实现它的方法。

我尝试使用def代码,但它落后于我的游戏。我尝试使用此网站:http://usingpython.com/pygame-tilemaps/

import pygame
import os
import sys
pygame.mixer.pre_init()
pygame.mixer.init(44100, 16, 2, 262144)
pygame.init()
from pygame.locals import*
import cv2
import time
import random
import pickle
import shutil

grey = (128,128,128)

def pause():

    paused = True

    while paused:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    paused = False
                elif event.key == pygame.K_SPACE:
                    menu(1)
        screen.fill(white)
        mts("Paused", black, -100, 100)
        mts("Press esc to go back to the game or press space to go back to the menu", black, 25, 45)
        pygame.display.update()
        clock.tick(60)

def score(score):
    text = pygame.font.Font('Fonts/Kuiper_Belt.otf', 25).render("Score: "+str(score), True, black)
    screen.blit(text, [0,0])

def highscore(highscore):
    text = pygame.font.Font('Fonts/Kuiper_Belt.otf', 25).render("Highscore: "+str(highscore), True, black)
    screen.blit(text, [1100,0])

#define the apple to spawn in a random place
def randAppleGen():
    randAppleX = random.randrange(0, dw-at, bs)
    randAppleY = random.randrange(0, dh-at, bs)

    return randAppleX,randAppleY

def snake(bs, sl):
    for XnY in sl:
        pygame.draw.rect(screen, Dgreen, [XnY[0],XnY[1],bs,bs])

def text_objects(text,color,fontS):
    font = pygame.font.Font('Fonts/Kuiper_Belt.otf', fontS)
    textSurface = font.render(text, True, color)
    return textSurface, textSurface.get_rect()

def mts(msg,color, y_displace=0, fs=35):
    textSurf, textRect = text_objects(msg,color,fs)
    textRect.center = (dw / 2), (dh / 2)+y_displace
    screen.blit(textSurf, textRect)

def gameLoop():
    global at
    global bs
    hs = pickle.load( open( os.getenv('APPDATA')+str('/Snake Universe/h.SNUN'), "rb" ) )
    gameExit = False
    gameOver = False
    gameHack = False
    tilemap = [
  [1,1,1,1],
  [1,0,0,1],
  [1,0,0,1],
  [1,1,1,1]
]

    WIDTH = 4 
    HEIGHT = 4
    TILESIZE = dw/WIDTH



    Speed = 20
    lead_x = dw/2
    lead_y = dh/2

    lead_x_change = 0
    lead_y_change = 0
    pygame.mixer.music.load(os.path.join(os.getcwd(), 'Sounds', 'music1.ogg'))
    pygame.mixer.music.play(-1) 

    slist = []
    sl = 0
    if sl > 2304:
        gameHack = True

    randAppleX,randAppleY = randAppleGen()

    while not gameExit:
        for row in range(HEIGHT):
            for column in range(WIDTH):
                if tilemap[row][column] == 1:
                    pygame.draw.rect(screen, grey, (column*TILESIZE, row*TILESIZE, TILESIZE, TILESIZE))

        while gameOver == True:
            screen.fill(white)
            mts("Game over", red, -50,100)
            mts("Press enter to play again or press space to go back to the menu", black, 50,50)
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    gameOver = False
                    gameExit = True
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_KP_ENTER or event.key==pygame.K_RETURN:
                        gameLoop()
                    if event.key == pygame.K_SPACE:
                        gameExit = False
                        gameOver = False
                        menu(1)
        while gameHack == True:
            pygame.mixer.music.stop()
            screen.fill(white)
            mts("Hacked", red, -50,100)
            mts("You hacked or exploit the game, press enter to quit the game", black, 50,50)
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    gameOver = False
                    gameExit = True
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_KP_ENTER or event.key==pygame.K_RETURN:
                        pygame.quit()
                        sys.exit()

        lead_x += lead_x_change
        lead_y += lead_y_change
        prev_x, prev_y = lead_x_change, lead_y_change 
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
                pygame.quit()
                sys.exit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT and prev_x != bs:
                    lead_x_change, lead_y_change = -bs, 0
                elif event.key == pygame.K_RIGHT and prev_x != -bs:
                    lead_x_change, lead_y_change = bs, 0
                elif event.key == pygame.K_UP and prev_y != bs:
                    lead_x_change, lead_y_change = 0, -bs
                elif event.key == pygame.K_DOWN and prev_y != -bs:
                    lead_x_change, lead_y_change = 0, bs    
                elif event.key == pygame.K_ESCAPE:
                    pause()
                elif event.key == pygame.K_s and Speed >= 10 and Speed < 60:
                    Speed += 10
                    clock.tick(Speed)
                elif event.key == pygame.K_d and Speed <= 60 and Speed > 10:
                    Speed -= 10
                    clock.tick(Speed)
                elif event.key == pygame.K_a:
                    sl += 1

        if not pygame.Rect(0, 0, dw, dh).contains(lead_x, lead_y, bs, bs):
            gameOver = True

        screen.fill(white)

        #draw the apple
        apple = pygame.draw.rect(screen, red, [randAppleX,randAppleY,at,at])

        sh = []
        sh.append(lead_x)
        sh.append(lead_y)
        slist.append(sh)
        snake(bs, slist)

        if len(slist) > sl:
            del slist[0]

        for eachSegment in slist[:-1]:
            if eachSegment == sh:
                gameOver = True

        score(sl)
        highscore(hs)

        if sl > hs:
            hs += 1
            os.remove( os.getenv('APPDATA')+str('/Snake Universe/h.SNUN') )
            pickle.dump( sl, open( os.getenv('APPDATA')+str('/Snake Universe/h.SNUN'), "wb" ) )
            hs = pickle.load( open( os.getenv('APPDATA')+str('/Snake Universe/h.SNUN'), "rb" ) )


        pygame.display.update()

        #make the apple spawn
        appleRect = pygame.Rect(randAppleX, randAppleY, at, at)
        if appleRect.collidepoint(lead_x, lead_y):
            while True:
                randAppleX, randAppleY = randAppleGen()
                appleRect = pygame.Rect(randAppleX, randAppleY, at, at)
                if not appleRect.collidepoint(lead_x, lead_y) and \
                not any(appleRect.collidepoint(*p) for p in slist):
                    break
            sl += 1
        clock.tick(Speed)
    pygame.quit()
    sys.quit()

我希望它显示单个彩色图块,但是我一直崩溃。

1 个答案:

答案 0 :(得分:0)

如果您已经在代码中包含问题并且在链接中包含示例,我不明白为什么会有问题。

.idea

编辑:

您有问题,因为您在错误的地方使用它。您必须在tilemap = [ [1,1,1,1], [1,0,0,1], [1,0,0,1], [1,1,1,1] ] MAPHEIGHT = 4 MAPWIDTH = 4 TILESIZE = dw/MAPWIDTH GREY = (128,128,128) for row in range(MAPHEIGHT): for column in range(MAPWIDTH): if tilemap[row][column] == 1: pygame.draw.rect(screen, GREY, column*TILESIZE, row*TILESIZE, TILESIZE, TILESIZE)) (清除缓冲区)和screen.fill(将缓冲区发送到视频卡并在屏幕上显示)之间进行绘制

pygame.update

您的代码混乱。您应该拥有

  • 在其中更新值,移动对象,板条箱/重排对象,加载/保存泡菜等的位置,但不要在此处绘制。
  • 在使用 screen.fill(white) for row in range(HEIGHT): for column in range(WIDTH): if tilemap[row][column] == 1: pygame.draw.rect(screen, grey, (column*TILESIZE, row*TILESIZE, TILESIZE, TILESIZE)) # ... draw other things ... pygame.display.update() 的地方,绘制所有元素,使用screen.fill,但不更新对象,也不加载/保存泡菜

编辑:

我的版本(具有更好的变量和函数名称)

pygame.display.update