Skip to main content
Code Review

Return to Revisions

2 of 6
added 948 characters in body

Here's a slight refactoring of your code to improve readability and reduce repetitiveness. (I don't think anything I'm doing here will do much for you in terms of performance, sadly.)

import curses as c
from itertools import product 
world_map = [
 '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
 '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
 '00000000000000000011111111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
 '00000000000000011111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
 '00000000000111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
 '00000000000011111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
 '00000000000000111111111111111111110000000000000000000000000000000001111111111111111111111111000000000000000000000000000',
 '00000000000000000111111111111111111111000000000000000000000000000001111111111111111111111111000000000000000000000000000',
 '00000000000000000011111111111111111000000000000000000000000000000001111133333111111111111111000000000000000000000000000',
 '00000000000000000000011111111111100000000000000000000000000000000001111133333111111111111111000000000000000000000000000',
 '00000000000000000000000000000000000000000000000000000000000000000001111133333111111111222211000000000000000000000000000',
 '00000000000000000000000000000000000000000000000000000000000000000001111133333111111111222211000000000000000000000000000',
 '00000000000000000000000000000000000000000000000000000000000000000001111133333111111111222211000000000000000000000000000',
 '00000000000000000000000000000000000000000000000000000011111111111111111133333111111111222211000000000000000000000000000',
 '00000000000000000000000000000000000000000000000000000011111111111111111133333111111111222211000000000000000000000000000',
 '00000000000000000000000000000000000000000000000000000011111111111111111111111111111111222211000000000000000000000000000',
 '00000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111000000000000000000000000000',
 '00000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111000000000000000000000000000',
 '00000000000000001111111111111111111111111000000000000011111111111110000000000000000000000000000000000000000000000000000',
 '00000000000000001111111111111111111111111000000000000011111111111110000000000000000000000000000000000000000000000000000',
 '00000000000000001114444411111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000',
 '00000000000000001114444411111115555555111000000000000000000000000000000000000000000000000000000000000000000000000000000',
 '00000000000000001114444411111115555555111000000000000000000000000000000000000000000000000000000000000000000000000000000',
 '00000000000000001114444411111115555555111000000000000000000000000000000000000000000000000000000000000000000000000000000',
 '00000000000000001111111111111115555555111000000000000000000000000000000000000000000000000000000000000000000000000000000',
 '00000000000000001111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000',
 '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
 '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
 '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
 '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
]
colors_for_initialising = (
 (255, 0, 0x64 * 1000 // 0xff, 0),
 (254, 0xff * 1000 // 0xff, 0xff * 1000 // 0xff, 0xff * 1000 // 0xff),
 (253, 0x82 * 1000 // 0xff, 0x8c * 1000 // 0xff, 0x51 * 1000 // 0xff),
 (252, 0xff * 1000 // 0xff, 0xe4 * 1000 // 0xff, 0xb5 * 1000 // 0xff),
 (251, 0xc0 * 1000 // 0xff, 0xc0 * 1000 // 0xff, 0xc0 * 1000 // 0xff)
)
RED, BLUE, GREEN = c.COLOR_RED, c.COLOR_BLUE, c.COLOR_GREEN
color_pairs = (
 (1, RED, BLUE), # water
 (2, RED, GREEN), # grass
 (3, RED, 255), # trees
 (4, RED, 254), # mountains
 (5, RED, 253), # swamp
 (6, RED, 252), # desert
 (7, RED, 251) # village
)
def win(stdscr):
 for color in colors_for_initialising:
 c.init_color(*color)
 for color_pair in color_pairs:
 c.init_pair(*color_pair)
 colors = [c.color_pair(i) for i in range(1, 8)]
 
 while True:
 for i, j in product(range(30), range(119)):
 stdscr.addch(i, j, ' ', colors[int(world_map[i][j])])
 stdscr.refresh()
def main():
 c.wrapper(win)
if __name__ == '__main__':
 main()

Summary of the changes I've made here:

  • Introduced an alias for curses (c) to make the code more concise. You can debate whether this change makes the code more or less readable — when you're using a module heavily, such as in this example, I generally prefer to use a shorter alias rather than having to type out the whole module name each time.
  • Also introduced aliases for colors later on in the code, for the same reason.
  • In your win function, you were calling curses.init_color and curses.init_pair repeatedly, which led to some repetitive code. I took the arguments for these calls out of win and put them into a tuple in the global namespace, then abstracted your series of calls to init_color and init_pair into two for-loops in win.
  • I took out your nested for-loop in win and replaced it with a call to itertools.product, which does the same thing but is more concise and (arguably) more readable.
default

AltStyle によって変換されたページ (->オリジナル) /