-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
74 lines (63 loc) · 2.04 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import Cpu
import Input
import pygame
import sys
cpu = Cpu.Cpu()
ROMPath = 'invaders.rom' # it should be smaller than 8192bytes
cpu.loadROM(ROMPath)
cpu.InitMap()
cpu.reset()
io = Input.Input()
pygame.init()
width, height = 256, 224
size = height, width
black = 0, 0, 0
surface = pygame.display.set_mode(size)
pygame.display.set_caption('Invaders')
surface.fill(black)
pxArray = pygame.PixelArray(surface)
def refresh(processor, px_array):
for i in range(height):
index = 0x2400 + (i << 5)
for j in range(32):
v_ram = processor.get_memory()[index]
index += 1
for k in range(8):
if (v_ram & 0x01) == 1:
px_array[i][255 - j * 8 - k] = (255, 255, 255)
else:
px_array[i][255 - j * 8 - k] = (0, 0, 0)
v_ram >>= 1
pygame.display.update()
fps = 60
fps_clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c: # coin
cpu.io.IN_PORT1 |= 0x01 # 1 player
if event.key == pygame.K_1:
cpu.io.IN_PORT1 |= 0x04
if event.key == pygame.K_SPACE: # fire
cpu.io.IN_PORT1 |= 0x10
if event.key == pygame.K_LEFT:
cpu.io.IN_PORT1 |= 0x20
if event.key == pygame.K_RIGHT:
cpu.io.IN_PORT1 |= 0x40
if event.type == pygame.KEYUP:
if event.key == pygame.K_c:
cpu.io.IN_PORT1 &= 255 - 0x01
if event.key == pygame.K_1:
cpu.io.IN_PORT1 &= 255 - 0x04
if event.key == pygame.K_SPACE:
cpu.io.IN_PORT1 &= 255 - 0x10
if event.key == pygame.K_LEFT:
cpu.io.IN_PORT1 &= 255 - 0x20
if event.key == pygame.K_RIGHT:
cpu.io.IN_PORT1 &= 255 - 0x40
cpu.Run()
refresh(cpu, pxArray)
fps_clock.tick(fps)
pygame.display.update()