简体   繁体   中英

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. 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 . 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM