Python pygame.locals() Examples
The following are 30
code examples of pygame.locals().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pygame
, or try the search function
.

Example #1
Source File: html.py From pgu with GNU Lesser General Public License v2.1 | 6 votes |
def __init__(self, data, globals=None, locals=None, loader=None, **params): super(HTML, self).__init__(**params) # This ensures that the whole HTML document is left-aligned within # the rendered surface. self.style.align = -1 _globals, _locals = globals, locals if _globals == None: _globals = {} if _locals == None: _locals = {} self._globals = _globals self._locals = _locals #font = gui.theme.get("label", "", "font") p = _html() p.init(self, self.style.font, self.style.color, _globals, _locals, loader=loader) p.feed(data) p.close() p.mydone()
Example #2
Source File: render_standard_text.py From SRNet-Datagen with Apache License 2.0 | 6 votes |
def render_normal(font, text): line_spacing = font.get_sized_height() + 1 line_bounds = font.get_rect(text) fsize = (round(2.0 * line_bounds.width), round(1.25 * line_spacing)) surf = pygame.Surface(fsize, pygame.locals.SRCALPHA, 32) x, y = 0, line_spacing rect = font.render_to(surf, (x, y), text) rect.x = x + rect.x rect.y = y - rect.y surf = pygame.surfarray.pixels_alpha(surf).swapaxes(0, 1) loc = np.where(surf > 20) miny, minx = np.min(loc[0]), np.min(loc[1]) maxy, maxx = np.max(loc[0]), np.max(loc[1]) return surf[miny:maxy+1, minx:maxx+1], rect
Example #3
Source File: code2.py From python101 with MIT License | 6 votes |
def handle_events(self): """ Obsługa zdarzeń systemowych, tutaj zinterpretujemy np. ruchy myszką :return True jeżeli pygame przekazał zdarzenie wyjścia z gry """ for event in pygame.event.get(): if event.type == pygame.locals.QUIT: pygame.quit() return True from pygame.locals import MOUSEMOTION, MOUSEBUTTONDOWN if event.type == MOUSEMOTION or event.type == MOUSEBUTTONDOWN: self.population.handle_mouse() # magiczne liczby używane do określenia czy komórka jest żywa
Example #4
Source File: code2a.py From python101 with MIT License | 6 votes |
def handle_events(self): """ Obsługa zdarzeń systemowych, tutaj zinterpretujemy np. ruchy myszką :return True jeżeli pygame przekazał zdarzenie wyjścia z gry """ for event in pygame.event.get(): if event.type == pygame.locals.QUIT: pygame.quit() return True from pygame.locals import MOUSEMOTION, MOUSEBUTTONDOWN if event.type == MOUSEMOTION or event.type == MOUSEBUTTONDOWN: self.population.handle_mouse() # magiczne liczby używane do określenia czy komórka jest żywa
Example #5
Source File: code0.py From python101 with MIT License | 6 votes |
def handle_events(self): """ Obsługa zdarzeń systemowych, tutaj zinterpretujemy np. ruchy myszką :return True jeżeli pygame przekazał zdarzenie wyjścia z gry """ for event in pygame.event.get(): if event.type == pygame.locals.QUIT: pygame.quit() return True from pygame.locals import MOUSEMOTION, MOUSEBUTTONDOWN if event.type == MOUSEMOTION or event.type == MOUSEBUTTONDOWN: self.population.handle_mouse() from pygame.locals import KEYDOWN, K_RETURN if event.type == KEYDOWN and event.key == K_RETURN: self.started = True # magiczne liczby używane do określenia czy komórka jest żywa
Example #6
Source File: tic_tac_toe.py From python101 with MIT License | 6 votes |
def handle_events(self): """ Obsługa zdarzeń systemowych, tutaj zinterpretujemy np. ruchy myszką :return True jeżeli pygame przekazał zdarzenie wyjścia z gry """ for event in pygame.event.get(): if event.type == pygame.locals.QUIT: pygame.quit() return True if event.type == pygame.locals.MOUSEBUTTONDOWN: if self.ai_turn: # jeśli jeszcze trwa ruch komputera to ignorujemy zdarzenia continue # pobierz aktualną pozycję kursora na planszy mierzoną w pikselach x, y = pygame.mouse.get_pos() self.board.player_move(x, y) self.ai_turn = True
Example #7
Source File: life.py From python101 with MIT License | 6 votes |
def handle_events(self): """ Obsługa zdarzeń systemowych, tutaj zinterpretujemy np. ruchy myszką :return True jeżeli pygame przekazał zdarzenie wyjścia z gry """ for event in pygame.event.get(): if event.type == pygame.locals.QUIT: pygame.quit() return True from pygame.locals import MOUSEMOTION, MOUSEBUTTONDOWN if event.type == MOUSEMOTION or event.type == MOUSEBUTTONDOWN: self.population.handle_mouse() from pygame.locals import KEYDOWN, K_RETURN if event.type == KEYDOWN and event.key == K_RETURN: self.started = True # magiczne liczby używane do określenia czy komórka jest żywa
Example #8
Source File: utils.py From pylot with Apache License 2.0 | 6 votes |
def run_visualizer_control_loop(control_display_stream): """Runs a pygame loop that waits for user commands. The user commands are send on the control_display_stream to control the pygame visualization window. """ import erdos import pygame clock = pygame.time.Clock() from pygame.locals import K_n while True: clock.tick_busy_loop(60) events = pygame.event.get() for event in events: if event.type == pygame.KEYUP: if event.key == K_n: control_display_stream.send( erdos.Message(erdos.Timestamp(coordinates=[0]), event.key))
Example #9
Source File: code1a.py From python101 with MIT License | 5 votes |
def handle_events(self): """ Obsługa zdarzeń systemowych, tutaj zinterpretujemy np. ruchy myszką :return True jeżeli pygame przekazał zdarzenie wyjścia z gry """ for event in pygame.event.get(): if event.type == pygame.locals.QUIT: pygame.quit() return True # magiczne liczby używane do określenia czy komórka jest żywa
Example #10
Source File: code1a.py From python101 with MIT License | 5 votes |
def draw_on(self, surface): """ Rysuje komórki na planszy """ for x, y in self.alive_cells(): size = (self.box_size, self.box_size) position = (x * self.box_size, y * self.box_size) color = (255, 255, 255) thickness = 1 pygame.draw.rect(surface, color, pygame.locals.Rect(position, size), thickness)
Example #11
Source File: code2.py From python101 with MIT License | 5 votes |
def draw_on(self, surface): """ Rysuje komórki na planszy """ for x, y in self.alive_cells(): size = (self.box_size, self.box_size) position = (x * self.box_size, y * self.box_size) color = (255, 255, 255) thickness = 1 pygame.draw.rect(surface, color, pygame.locals.Rect(position, size), thickness)
Example #12
Source File: code1.py From python101 with MIT License | 5 votes |
def handle_events(self): """ Obsługa zdarzeń systemowych, tutaj zinterpretujemy np. ruchy myszką :return True jeżeli pygame przekazał zdarzenie wyjścia z gry """ for event in pygame.event.get(): if event.type == pygame.locals.QUIT: pygame.quit() return True # Ta część powinna być zawsze na końcu modułu (ten plik jest modułem) # chcemy uruchomić naszą grę dopiero po tym jak wszystkie klasy zostaną zadeklarowane
Example #13
Source File: code3.py From python101 with MIT License | 5 votes |
def draw_on(self, surface): """ Rysuje komórki na planszy """ for x, y in self.alive_cells(): size = (self.box_size, self.box_size) position = (x * self.box_size, y * self.box_size) color = (255, 255, 255) thickness = 1 pygame.draw.rect(surface, color, pygame.locals.Rect(position, size), thickness)
Example #14
Source File: code2a.py From python101 with MIT License | 5 votes |
def draw_on(self, surface): """ Rysuje komórki na planszy """ for x, y in self.alive_cells(): size = (self.box_size, self.box_size) position = (x * self.box_size, y * self.box_size) color = (255, 255, 255) thickness = 1 pygame.draw.rect(surface, color, pygame.locals.Rect(position, size), thickness)
Example #15
Source File: code0.py From python101 with MIT License | 5 votes |
def draw_on(self, surface): """ Rysuje komórki na planszy """ for x, y in self.alive_cells(): size = (self.box_size, self.box_size) position = (x * self.box_size, y * self.box_size) color = (255, 255, 255) thickness = 1 pygame.draw.rect(surface, color, pygame.locals.Rect(position, size), thickness)
Example #16
Source File: pong_z2.py From python101 with MIT License | 5 votes |
def handle_events(self): """ Obsługa zdarzeń systemowych, tutaj zinterpretujemy np. ruchy myszką :return True jeżeli pygame przekazał zdarzenie wyjścia z gry """ for event in pygame.event.get(): if event.type == pygame.locals.QUIT: pygame.quit() return True # Ta część powinna być zawsze na końcu modułu (ten plik jest modułem) # chcemy uruchomić naszą grę dopiero po tym jak wszystkie klasy zostaną zadeklarowane
Example #17
Source File: pong_z5.py From python101 with MIT License | 5 votes |
def handle_events(self): """ Obsługa zdarzeń systemowych, tutaj zinterpretujemy np. ruchy myszką :return True jeżeli pygame przekazał zdarzenie wyjścia z gry """ for event in pygame.event.get(): if event.type == pygame.locals.QUIT: pygame.quit() return True if event.type == pygame.locals.MOUSEMOTION: # myszka steruje ruchem pierwszego gracza x, y = event.pos self.player1.move(x)
Example #18
Source File: pong_z7.py From python101 with MIT License | 5 votes |
def handle_events(self): """ Obsługa zdarzeń systemowych, tutaj zinterpretujemy np. ruchy myszką :return True jeżeli pygame przekazał zdarzenie wyjścia z gry """ for event in pygame.event.get(): if event.type == pygame.locals.QUIT: pygame.quit() return True if event.type == pygame.locals.MOUSEMOTION: # myszka steruje ruchem pierwszego gracza x, y = event.pos self.player1.move(x)
Example #19
Source File: pong_z6.py From python101 with MIT License | 5 votes |
def handle_events(self): """ Obsługa zdarzeń systemowych, tutaj zinterpretujemy np. ruchy myszką :return True jeżeli pygame przekazał zdarzenie wyjścia z gry """ for event in pygame.event.get(): if event.type == pygame.locals.QUIT: pygame.quit() return True if event.type == pygame.locals.MOUSEMOTION: # myszka steruje ruchem pierwszego gracza x, y = event.pos self.player1.move(x)
Example #20
Source File: pong_z4.py From python101 with MIT License | 5 votes |
def handle_events(self): """ Obsługa zdarzeń systemowych, tutaj zinterpretujemy np. ruchy myszką :return True jeżeli pygame przekazał zdarzenie wyjścia z gry """ for event in pygame.event.get(): if event.type == pygame.locals.QUIT: pygame.quit() return True
Example #21
Source File: pong.py From python101 with MIT License | 5 votes |
def handle_events(self): """ Obsługa zdarzeń systemowych, tutaj zinterpretujemy np. ruchy myszką :return True jeżeli pygame przekazał zdarzenie wyjścia z gry """ for event in pygame.event.get(): if event.type == pygame.locals.QUIT: pygame.quit() return True if event.type == pygame.locals.MOUSEMOTION: # myszka steruje ruchem pierwszego gracza x, y = event.pos self.player1.move(x)
Example #22
Source File: life.py From python101 with MIT License | 5 votes |
def draw_on(self, surface): """ Rysuje komórki na planszy """ for x, y in self.alive_cells(): size = (self.box_size, self.box_size) position = (x * self.box_size, y * self.box_size) color = (255, 255, 255) thickness = 1 pygame.draw.rect(surface, color, pygame.locals.Rect(position, size), thickness)
Example #23
Source File: snack_pygame.py From Python-Application with GNU General Public License v3.0 | 5 votes |
def press(keys, snack): global score # K_w 为 pygame.locals 中的常量 # keys[K_w] 返回 True or False # 上移 if keys[K_w] or keys[K_UP]: snack.toward(0, -1) # 下移 elif keys[K_s] or keys[K_DOWN]: snack.toward(0, 1) # 左移 elif keys[K_a] or keys[K_LEFT]: snack.toward(-1, 0) # 右移 elif keys[K_d] or keys[K_RIGHT]: snack.toward(1, 0) # 重置游戏 elif keys[K_r]: score = 0 main() # 退出游戏 elif keys[K_ESCAPE]: exit() # 游戏初始化
Example #24
Source File: scrap_test.py From fxxkpython with GNU General Public License v3.0 | 5 votes |
def test_issue_208(self): """PATCH: pygame.scrap on X11, fix copying into PRIMARY selection Copying into theX11 PRIMARY selection (mouse copy/paste) would not work due to a confusion between content type and clipboard type. """ from pygame import display, event, freetype from pygame.locals import SCRAP_SELECTION, SCRAP_TEXT from pygame.locals import KEYDOWN, K_y, QUIT success = False freetype.init() font = freetype.Font(None, 24) display.init() display.set_caption("Interactive X11 Paste Test") screen = display.set_mode((600, 200)) screen.fill(pygame.Color('white')) text = "Scrap put() succeeded." msg = ('Some text has been placed into the X11 clipboard.' ' Please click the center mouse button in an open' ' text window to retrieve it.' '\n\nDid you get "{}"? (y/n)').format(text) word_wrap(screen, msg, font, 6) display.flip() event.pump() scrap.init() scrap.set_mode(SCRAP_SELECTION) scrap.put(SCRAP_TEXT, text.encode('UTF-8')) while True: e = event.wait() if e.type == QUIT: break if e.type == KEYDOWN: success = (e.key == K_y) break pygame.display.quit() self.assertTrue(success)
Example #25
Source File: scrap_test.py From fxxkpython with GNU General Public License v3.0 | 5 votes |
def test_issue_208(self): """PATCH: pygame.scrap on X11, fix copying into PRIMARY selection Copying into theX11 PRIMARY selection (mouse copy/paste) would not work due to a confusion between content type and clipboard type. """ from pygame import display, event, freetype from pygame.locals import SCRAP_SELECTION, SCRAP_TEXT from pygame.locals import KEYDOWN, K_y, QUIT success = False freetype.init() font = freetype.Font(None, 24) display.init() display.set_caption("Interactive X11 Paste Test") screen = display.set_mode((600, 200)) screen.fill(pygame.Color('white')) text = "Scrap put() succeeded." msg = ('Some text has been placed into the X11 clipboard.' ' Please click the center mouse button in an open' ' text window to retrieve it.' '\n\nDid you get "{}"? (y/n)').format(text) word_wrap(screen, msg, font, 6) display.flip() event.pump() scrap.init() scrap.set_mode(SCRAP_SELECTION) scrap.put(SCRAP_TEXT, text.encode('UTF-8')) while True: e = event.wait() if e.type == QUIT: break if e.type == KEYDOWN: success = (e.key == K_y) break pygame.display.quit() self.assertTrue(success)
Example #26
Source File: scrap_test.py From fxxkpython with GNU General Public License v3.0 | 5 votes |
def test_issue_208(self): """PATCH: pygame.scrap on X11, fix copying into PRIMARY selection Copying into theX11 PRIMARY selection (mouse copy/paste) would not work due to a confusion between content type and clipboard type. """ from pygame import display, event, freetype from pygame.locals import SCRAP_SELECTION, SCRAP_TEXT from pygame.locals import KEYDOWN, K_y, QUIT success = False freetype.init() font = freetype.Font(None, 24) display.init() display.set_caption("Interactive X11 Paste Test") screen = display.set_mode((600, 200)) screen.fill(pygame.Color('white')) text = "Scrap put() succeeded." msg = ('Some text has been placed into the X11 clipboard.' ' Please click the center mouse button in an open' ' text window to retrieve it.' '\n\nDid you get "{}"? (y/n)').format(text) word_wrap(screen, msg, font, 6) display.flip() event.pump() scrap.init() scrap.set_mode(SCRAP_SELECTION) scrap.put(SCRAP_TEXT, text.encode('UTF-8')) while True: e = event.wait() if e.type == QUIT: break if e.type == KEYDOWN: success = (e.key == K_y) break pygame.display.quit() self.assertTrue(success)
Example #27
Source File: scrap_test.py From fxxkpython with GNU General Public License v3.0 | 5 votes |
def test_issue_208(self): """PATCH: pygame.scrap on X11, fix copying into PRIMARY selection Copying into theX11 PRIMARY selection (mouse copy/paste) would not work due to a confusion between content type and clipboard type. """ from pygame import display, event, freetype from pygame.locals import SCRAP_SELECTION, SCRAP_TEXT from pygame.locals import KEYDOWN, K_y, QUIT success = False freetype.init() font = freetype.Font(None, 24) display.init() display.set_caption("Interactive X11 Paste Test") screen = display.set_mode((600, 200)) screen.fill(pygame.Color('white')) text = "Scrap put() succeeded." msg = ('Some text has been placed into the X11 clipboard.' ' Please click the center mouse button in an open' ' text window to retrieve it.' '\n\nDid you get "{}"? (y/n)').format(text) word_wrap(screen, msg, font, 6) display.flip() event.pump() scrap.init() scrap.set_mode(SCRAP_SELECTION) scrap.put(SCRAP_TEXT, text.encode('UTF-8')) while True: e = event.wait() if e.type == QUIT: break if e.type == KEYDOWN: success = (e.key == K_y) break pygame.display.quit() self.assertTrue(success)
Example #28
Source File: image_object_detection_video_drone_multiprocessing.py From TensorFlow-Tello-Object_Detection- with GNU General Public License v3.0 | 4 votes |
def joystick(ns, event): pygame.init() pygame.joystick.init() try: js = pygame.joystick.Joystick(0) js.init() js_name = js.get_name() print('Joystick name: ' + js_name) if js_name in ('Wireless Controller', 'Sony Computer Entertainment Wireless Controller'): buttons = JoystickPS4 elif js_name in ('PLAYSTATION(R)3 Controller', 'Sony PLAYSTATION(R)3 Controller'): buttons = JoystickPS3 elif js_name == 'Xbox One Wired Controller': buttons = JoystickXONE except pygame.error: pass if buttons is None: print('no supported joystick found') return try: while True: time.sleep(0.01) for e in pygame.event.get(): #if e.type == pygame.locals.JOYAXISMOTION: if e.type == pygame.locals.JOYHATMOTION: ns.type = e.type ns.value = e.value ns.button = None elif e.type == pygame.locals.JOYBUTTONDOWN: ns.type = e.type ns.value = None ns.button = e.button elif e.type == pygame.locals.JOYBUTTONUP: ns.type = e.type ns.value = None ns.button = e.button #print(ns.type, " - ", ns.value, " - ", ns.button) event.set() except Exception as e: print(e)
Example #29
Source File: object_detection_video_drone_multiprocessing.py From TensorFlow-Tello-Object_Detection- with GNU General Public License v3.0 | 4 votes |
def joystick(ns, event): pygame.init() pygame.joystick.init() try: js = pygame.joystick.Joystick(0) js.init() js_name = js.get_name() print('Joystick name: ' + js_name) if js_name in ('Wireless Controller', 'Sony Computer Entertainment Wireless Controller'): buttons = JoystickPS4 elif js_name in ('PLAYSTATION(R)3 Controller', 'Sony PLAYSTATION(R)3 Controller'): buttons = JoystickPS3 elif js_name == 'Xbox One Wired Controller': buttons = JoystickXONE except pygame.error: pass if buttons is None: print('no supported joystick found') return try: while True: time.sleep(0.01) for e in pygame.event.get(): #if e.type == pygame.locals.JOYAXISMOTION: if e.type == pygame.locals.JOYHATMOTION: ns.type = e.type ns.value = e.value ns.button = None elif e.type == pygame.locals.JOYBUTTONDOWN: ns.type = e.type ns.value = None ns.button = e.button elif e.type == pygame.locals.JOYBUTTONUP: ns.type = e.type ns.value = None ns.button = e.button #print(ns.type, " - ", ns.value, " - ", ns.button) event.set() except Exception as e: print(e)
Example #30
Source File: render_text_mask.py From SRNet-Datagen with Apache License 2.0 | 4 votes |
def render_normal(font, text): # get the number of lines lines = text.split('\n') lengths = [len(l) for l in lines] # font parameters: line_spacing = font.get_sized_height() + 1 # initialize the surface to proper size: line_bounds = font.get_rect(lines[np.argmax(lengths)]) fsize = (round(2.0 * line_bounds.width), round(1.25 * line_spacing * len(lines))) surf = pygame.Surface(fsize, pygame.locals.SRCALPHA, 32) bbs = [] space = font.get_rect('O') x, y = 0, 0 for l in lines: x = 0 # carriage-return y += line_spacing # line-feed for ch in l: # render each character if ch.isspace(): # just shift x += space.width else: # render the character ch_bounds = font.render_to(surf, (x,y), ch) ch_bounds.x = x + ch_bounds.x ch_bounds.y = y - ch_bounds.y x += ch_bounds.width bbs.append(np.array(ch_bounds)) # get the union of characters for cropping: r0 = pygame.Rect(bbs[0]) rect_union = r0.unionall(bbs) # get the words: words = ' '.join(text.split()) # crop the surface to fit the text: bbs = np.array(bbs) surf_arr, bbs = crop_safe(pygame.surfarray.pixels_alpha(surf), rect_union, bbs, pad=5) surf_arr = surf_arr.swapaxes(0,1) #self.visualize_bb(surf_arr,bbs) return surf_arr, bbs