Pygame关卡编辑器

时间:2015-07-07 18:06:49

标签: python pygame

我目前在pygame中制作平台游戏,这是我通过教程和无尽搜索构建的小编辑器。事情是,一切都很好,但我正在寻找一种方法来保存每次的矩形颜色,然后保存并像其他属性一样导出它,我希望每个矩形保存其颜色,而不是像它现在如何工作和更改to_draw中包含的所有rects的颜色。如果你帮了我,我会很高兴,谢谢!

import pygame
from colors import *
pygame.init()
file = open('levels/level.txt', 'w')
size = width, height = 1280,960
window = pygame.display.set_mode(size)
pygame.display.set_caption( "platformer editor v0.02" )

clock = pygame.time.Clock()
fps = 60

to_draw = []
current_color = 0
draw_start = False

##GRAMMATA##
font = pygame.font.Font(None, 36)
def show_text(msg,color,x,y):
    text = font.render(msg,True,color)
    textpos = (x,y)
    window.blit(text,textpos)
############


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

        if event.type == pygame.MOUSEMOTION:
            mouse_pos = mouse_x, mouse_y = pygame.mouse.get_pos()

        if event.type == pygame.MOUSEBUTTONDOWN:
            pos = mouse_pos
            draw_start = True

        if event.type == pygame.MOUSEBUTTONUP:
            final_pos = mouse_pos
            draw_start = False

            rect = pygame.Rect(pos,(final_pos[0]- pos[0], final_pos[1]-pos[1]))
            rect.normalize()
            to_draw += [rect]

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False
            if event.key == pygame.K_LEFT:
                current_color += 1
            if event.key == pygame.K_RIGHT:
                current_color -= 1

            if event.key == pygame.K_RETURN:
                for platform in to_draw:

                    print("["+str(platform).split("(")[1].split(")")[0] +",black ],")
                    file.write("["+str(platform).split("(")[1].split(")")[0] +",black ]\n")
            if event.key == pygame.K_BACKSPACE:
                to_draw.pop()
    window.fill(white)

    if current_color not in [0,1,2,3,4]:
        current_color = 0

    if draw_start:
        pygame.draw.rect(window,red, pygame.Rect(pos, (mouse_pos[0] - pos[0],mouse_pos[1]- pos[1])))
    for item in to_draw:
        pygame.draw.rect(window,color_list[current_color],item)


    show_text("x:",red, 1070,0)
    show_text(str(mouse_x),red,1100,0)
    show_text("y:",red,1170,0)
    show_text(str(mouse_y),red,1200,0)

    pygame.display.update()
    clock.tick(fps)

file.close()
pygame.quit()

1 个答案:

答案 0 :(得分:0)

我没有测试该代码,但它应该可以工作。我正在使用元组来存储值

class ObjectOne(models.Model):
    other-objecs = models.ManyToManyField(OtherObject)

class OtherObject(models.Model):
    somefield = models.TextField()