-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpygameApplikasjon.py
147 lines (124 loc) · 5.31 KB
/
pygameApplikasjon.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import pygame as pg
import sys
import os
if getattr(sys, 'frozen', False):
os.chdir(os.path.dirname(sys.executable))
from predict import generate_text
pg.init()
pg.key.set_repeat(300, 25)
screen = pg.display.set_mode((700, 750))
COLOR_INACTIVE = pg.Color('black')
COLOR_ACTIVE = pg.Color('white')
FONT = pg.font.Font("fonts/OpenSans-Regular.ttf",20)#pg.font.SysFont('arial', 20)
class InputBox:
def __init__(self, x, y, w, h, text=''):
self.rect = pg.Rect(x, y, w, h)
self.color = COLOR_INACTIVE
self.text = text
self.txt_surface = FONT.render(text, True, pg.Color('black'))
self.active = False
def handle_event(self, event):
if event.type == pg.MOUSEBUTTONDOWN:
# If the user clicked on the input_box rect.
if self.rect.collidepoint(event.pos):
# Toggle the active variable.
self.active = not self.active
else:
self.active = False
# Change the current color of the input box.
self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
if event.type == pg.KEYDOWN:
if self.active:
if event.key == pg.K_RETURN:
pass
elif event.key == pg.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
# Re-render the text.
self.txt_surface = FONT.render(self.text, True, pg.Color('black'))
def update(self):
# Resize the box if the text is too long.
width = max(100, self.txt_surface.get_width() + 10)
self.rect.w = width
def draw(self, screen):
# Blit the text.
screen.blit(self.txt_surface, (self.rect.x + 5, self.rect.y + 5))
# Blit the rect.
pg.draw.rect(screen, self.color, self.rect, 2)
def getWidth(self):
return self.rect.w
def getText(self):
return self.text
class Background(pg.sprite.Sprite):
def __init__(self, image_file, location):
pg.sprite.Sprite.__init__(self) #call Sprite initializer
self.image = pg.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
def blit_text(surface, text, pos, font, boxWidth, color=pg.Color('black')):
words = [word.split(' ') for word in text.splitlines()] # 2D array where each row is a list of words.
space = font.size(' ')[0] # The width of a space.
max_width, max_height = surface.get_size()
x, y = pos
x = boxWidth+20
for line in words:
for word in line:
word_surface = font.render(word, 0, color)
word_width, word_height = word_surface.get_size()
if x + word_width >= max_width:
x = pos[0] # Reset the x.
y += word_height # Start on new row.
surface.blit(word_surface, (x, y))
x += word_width + space
x = pos[0] # Reset the x.
y += word_height # Start on new row.
def main():
clock = pg.time.Clock()
input_box1 = InputBox(15, 100, 70, 30)
input_boxes = [input_box1]
BackGround = Background("images/blankTrumpTweetWComment.png", [0,0])
tweetButton = pg.image.load('images/button.png')
done = False
text = "This is a really long sentence with a couple of breaks.\nSometimes it will break even if there isn't a break " \
"in the sentence, but that's because the text is too long to fit the screen.\nIt can look strange sometimes.\n" \
"This function doesn't check if the text is too high to fit on the height of the surface though, so sometimes " \
"text will disappear underneath the surface"
font = pg.font.Font("fonts/OpenSans-Bold.ttf",20)#pg.font.SysFont('arial', 20)
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
if pg.mouse.get_pos()[0] > 550 and pg.mouse.get_pos()[0] < 650:
if pg.mouse.get_pos()[1] > 300 and pg.mouse.get_pos()[1] < 350:
try:
text = generate_text([input_box1.getText()])[0]
except Exception as e:
text = "Trump skjønner ikke dette ordet"
print(e)
if event.type == pg.KEYDOWN:
if event.key == pg.K_CLEAR:
text = ''
elif event.key == pg.K_RETURN:
try:
text = generate_text([input_box1.getText()])[0]
except:
text = "Trump skjønner ikke dette ordet"
for box in input_boxes:
box.handle_event(event)
for box in input_boxes:
box.update()
screen.fill((255, 255, 255))
screen.blit(BackGround.image, BackGround.rect)
screen.blit(tweetButton, (550,300))
#print text
blit_text(screen, text, (15, 105), font, input_box1.getWidth())
commentText = 'hello'
blit_text(screen, commentText, (0,550), font, 71)
for box in input_boxes:
box.draw(screen)
pg.display.flip()
clock.tick(30)
main()
pg.quit()