简体   繁体   English

需要帮助更新屏幕

[英]Need Help Updating Screen

I am working on making a game that creates a world and has entities that move in it. 我正在努力创造一个创造世界并在其中移动实体的游戏。 Before I implement movement, I was wondering how I could update the screen differently than reprinting the world over and over again. 在实施移动功能之前,我想知道如何与一遍又一遍地重印世界不同地更新屏幕。 Also, I don't want to use modules like pygame when creating this game. 另外,在创建此游戏时,我不想使用像pygame这样的模块。 The code is below: 代码如下:

import time

#Global Constants
spino = "X"
player = "O"
empty = " "
world_size = 102

def new_world():
    world = []
    for square in range(world_size):
        world.append(empty)
    return world

def spawn_spinos(world):
    world[0] = spino
    world[20] = spino
    world[40] = spino
    world[60] = spino
    world[80] = spino
    return world

def spawn_player(world):
    world[44] = player
    return world

def display_world(world):
   print (world[0], "|", world[1], "|", world[2], "|", world[3], "|", world[4], "|", world[5], "|", world[6], "|", world[7], "|", world[8], "|", world[9])
   print (world[10], "|", world[11], "|", world[12], "|", world[13], "|", world[14], "|", world[15], "|", world[16], "|", world[17], "|", world[18], "|", world[19])
   print (world[20], "|", world[21], "|", world[22], "|", world[23], "|", world[24], "|", world[25], "|", world[26], "|", world[27], "|", world[28], "|", world[29])
   print (world[30], "|", world[31], "|", world[32], "|", world[33], "|", world[34], "|", world[35], "|", world[36], "|", world[37], "|", world[38], "|", world[39])
   print (world[40], "|", world[41], "|", world[42], "|", world[43], "|", world[44], "|", world[45], "|", world[46], "|", world[47], "|", world[48], "|", world[49])
   print (world[50], "|", world[51], "|", world[52], "|", world[53], "|", world[54], "|", world[55], "|", world[56], "|", world[57], "|", world[58], "|", world[59])
   print (world[60], "|", world[61], "|", world[62], "|", world[63], "|", world[64], "|", world[65], "|", world[66], "|", world[67], "|", world[68], "|", world[69])
   print (world[70], "|", world[71], "|", world[72], "|", world[73], "|", world[74], "|", world[75], "|", world[76], "|", world[77], "|", world[78], "|", world[79])
   print (world[80], "|", world[81], "|", world[82], "|", world[83], "|", world[84], "|", world[85], "|", world[86], "|", world[87], "|", world[88], "|", world[89])
   print (world[90], "|", world[91], "|", world[92], "|", world[93], "|", world[94], "|", world[95], "|", world[96], "|", world[97], "|", world[98], "|", world[99])

def update_world(world):
    while True:
        display_world(world)
        time.sleep(1)


world = new_world()
world = spawn_spinos(world)
world = spawn_player(world)
update_world(world)

To handle screen drawing, try curses . 要处理屏幕绘制,请尝试使用curses It's been around a long time, and is probably the screen handling library used by most full-screen terminal programs. 它已经存在了很长时间,并且可能是大多数全屏终端程序使用的屏幕处理库。

Here are some snippets from working code based on your program: 以下是基于您的程序的工作代码片段:

from curses import wrapper

def display_world(world, stdscr):
    for y in range(0, 10):
        for x in range(0, 10):
            stdscr.addch(y+1, 2*x, world[y*10+x])
            stdscr.addch(y+1, 2*x+1, "|")
    stdscr.refresh()
    key = stdscr.getkey()
    stdscr.addnstr(0, 3, key, 10)
    stdscr.refresh()
    if key is "q":
        exit(0)

def update_world(world, stdscr):
    while True:
        display_world(world, stdscr)
        sleep(0.1)

def main(stdscr):
    stdscr.clear()
    world = new_world()
    world = spawn_spinos(world)
    world = spawn_player(world)
    update_world(world, stdscr)

wrapper(main)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每分钟需要帮助更新tkinter函数 - Need help updating a tkinter function every minute 需要帮助在pygame中创建动态更新分数 - Need help creating a dynamically updating score in pygame 需要帮助以Raspberry和Python控制LCD屏幕 - Need help to control LCD screen with Raspberry and Python 我需要帮助使用 tkinter 显示全屏图像 - I need help displaying full screen images with tkinter 我需要帮助调试 kivy 屏幕管理器代码 - I need help debugging kivy screen manager code 为什么 Kivy 在我需要时不刷新/更新我的屏幕? - Why Kivy is not refreshing/updating my screen when I need it? 需要帮助将GLSL 120 gl_ModelViewProjectionMatrix代码更新为GLSL 330,shaderMatrix不会在着色器中更新 - Need help updating GLSL 120 gl_ModelViewProjectionMatrix code to GLSL 330, uniformMatrix isn't updating in shader 当玩家进入房间时需要帮助打印“物品”,更新库存列表并输掉游戏 - Need help printint the 'item' when the player enters the room, updating the Inventory list and lose game OOP 和 python 的新手,需要帮助更新我的 __init__ function 中的 self.lifepoints, - New to OOP with python, need help updating a self.lifepoints within my __init__ function, 并行运行appium测试时视频的屏幕录像(需要线程帮助) - Screen recording of videos while running appium test in parallel (Need help in threading)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM