-
Notifications
You must be signed in to change notification settings - Fork 5
/
environment_render.py
70 lines (55 loc) · 1.87 KB
/
environment_render.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
import pygame
from pygame.locals import *
SCREENRECT = Rect(0, 0, 640, 480)
class Textsprite(pygame.sprite.Sprite):
def __init__(self, text):
pygame.sprite.Sprite.__init__(self)
self.text = text
self.pos = 0
self.update()
def update(self):
self.image = pygame.font.Font(None, 36).render(self.text, 1, (0, 0, 0))
self.highlight = pygame.font.Font(None, 36).render(self.text[:self.pos], 1, (0, 0, 255))
self.image.blit(self.highlight, (0, 0))
self.rect = self.image.get_rect()
self.rect.center = pygame.display.get_surface().get_rect().center
def keyin(self, key):
if key == self.text[self.pos]:
self.pos = self.pos + 1
if len(self.text) == self.pos:
self.pos = 0
def main():
pygame.init()
screen = pygame.display.set_mode(SCREENRECT.size)
# make background
background = pygame.Surface(SCREENRECT.size).convert()
background.fill((255, 255, 255))
screen.blit(background, (0, 0))
pygame.display.update()
# keep track of sprites
all = pygame.sprite.RenderUpdates()
# keep track of time
clock = pygame.time.Clock()
textsprite = Textsprite('The quick brown fox jumps over the lazy dog')
all.add(textsprite)
# game loop
while 1:
# get input
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
return
else:
textsprite.keyin(event.unicode)
# clear sprites
all.clear(screen, background)
# update sprites
all.update()
# redraw sprites
dirty = all.draw(screen)
pygame.display.update(dirty)
# maintain frame rate
clock.tick(30)
if __name__ == '__main__': main()