input in pygame

Solutions on MaxInterview for input in pygame by the best coders in the world

showing results for - "input in pygame"
Anton
25 Nov 2016
1import pygame as pg
2
3
4pg.init()
5screen = pg.display.set_mode((640, 480))
6COLOR_INACTIVE = pg.Color('lightskyblue3')
7COLOR_ACTIVE = pg.Color('dodgerblue2')
8FONT = pg.font.Font(None, 32)
9
10
11class InputBox:
12
13    def __init__(self, x, y, w, h, text=''):
14        self.rect = pg.Rect(x, y, w, h)
15        self.color = COLOR_INACTIVE
16        self.text = text
17        self.txt_surface = FONT.render(text, True, self.color)
18        self.active = False
19
20    def handle_event(self, event):
21        if event.type == pg.MOUSEBUTTONDOWN:
22            # If the user clicked on the input_box rect.
23            if self.rect.collidepoint(event.pos):
24                # Toggle the active variable.
25                self.active = not self.active
26            else:
27                self.active = False
28            # Change the current color of the input box.
29            self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
30        if event.type == pg.KEYDOWN:
31            if self.active:
32                if event.key == pg.K_RETURN:
33                    print(self.text)
34                    self.text = ''
35                elif event.key == pg.K_BACKSPACE:
36                    self.text = self.text[:-1]
37                else:
38                    self.text += event.unicode
39                # Re-render the text.
40                self.txt_surface = FONT.render(self.text, True, self.color)
41
42    def update(self):
43        # Resize the box if the text is too long.
44        width = max(200, self.txt_surface.get_width()+10)
45        self.rect.w = width
46
47    def draw(self, screen):
48        # Blit the text.
49        screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
50        # Blit the rect.
51        pg.draw.rect(screen, self.color, self.rect, 2)
52
53
54
55def main():
56    clock = pg.time.Clock()
57    input_box1 = InputBox(100, 100, 140, 32)
58    input_box2 = InputBox(100, 300, 140, 32)
59    input_boxes = [input_box1, input_box2]
60    done = False
61
62    while not done:
63        for event in pg.event.get():
64            if event.type == pg.QUIT:
65                done = True
66            for box in input_boxes:
67                box.handle_event(event)
68
69        for box in input_boxes:
70            box.update()
71
72        screen.fill((30, 30, 30))
73        for box in input_boxes:
74            box.draw(screen)
75
76        pg.display.flip()
77        clock.tick(30)
78
79
80if __name__ == '__main__':
81    main()
82    pg.quit()
Marceau
07 Jan 2019
1def getInput():
2    ''
3    for event in pygame.event.get():
4            
5            if event.type == pygame.KEYDOWN:
6                print("event" + str(event))
7                if event.key == pygame.K_ESCAPE:
8                    playing = False
9                elif event.key == pygame.K_w:
10                    input = input + "w"
11                elif event.key == pygame.K_s:
12                    input = input + "s"
13                if event.key == pygame.K_a:
14                    input = input + "a"
15                    
Caterina
04 Jul 2019
1text input pygame
queries leading to this page
pygame number inputtext input pygamesimple pygame inputpygame inputhow to get a text box in pygamepygame input textepython pygame input boxhow to make text input in pygamepygame gui textboxhow to make text field pygamecan you input in pygamecreate input input pygamepython pygame tutorial getting text inputpygame textboxpygame get text inputpygame input fieldhow to get player input in pygame pythonhow to draw a text input pygamecode to make a text box with input in pygametext input box pygamepygame text fieldpygame inputssimple input button in pygameenter in pygame textpygame text boxpygame user input texthow to make imputs show up in a rect pygameinput box in pygamepygame enter textinput box pygamehow to maek a text box in oygamehow to create box inputs in pygamepygame how to make inputhow to make a input box in pygamehow to keep a number input box in pygameinput pygamehow to take input from user in pygamegetting text input pygame pythonall input pygametkinter text box pre textpygame how to get inputtext input in pygamepygame add input promptinput box code pygamehow to make a textbox in pygamepygame input windowuser input in pygamecode to display text with input in pygamepygame input text boxpython pygame text box inputget input pygamehow can i create a text input box with pygameinputbox pygameusing pygame for inputinput text in pygametextbox in pygamehow to make a text box in pygametexbox python pygamehow to accept user input in pygametextbox pygamehow to create an input box in pygamewrite player in box input in pygamea board game and input text box in pygameinput in pygametext box input pygamepygame text bozhow to create a text box in python pygamepygame input texthow to draw text box pygameuser input pygamehow to add input text box in pygamehow to get input in pygamepygame iteractive textpygame textinputpygame input tutorialpygame input simplepygame text inputpygame gui user text inputhow to input text in pygamehow to make inputs in pygamedisplay input text in pygameinput button in pygamegetting input pygamepygame text input fieldsget text input pygamepygame getting text inputinput for pygamepygame input fieldshow to make an input box in pygamehowt put input in pygamehow to get text input with pygameinput text pygamehow to create a input box in pygamehow to make a text input box python pygamehow to make input in pygamepygame input boxinput in pygame