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 | 7 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: 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 #4
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 #5
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 #6
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 #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: 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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
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 #22
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 #23
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 #24
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 #25
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 #26
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 #27
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 #28
Source File: lincoln.py From pylot with Apache License 2.0 | 4 votes |
def main(argv): (obstacles_stream, traffic_lights_stream, obstacles_tracking_stream, open_drive_stream, global_trajectory_stream, control_display_stream, streams_to_send_top_on) = create_data_flow() # Run the data-flow. erdos.run_async() # Send waypoints. waypoints = Waypoints.read_from_csv_file(FLAGS.waypoints_csv_file, FLAGS.target_speed) global_trajectory_stream.send( erdos.Message(erdos.Timestamp(coordinates=[0]), waypoints)) # Send top watermark on all streams that require it. top_msg = erdos.WatermarkMessage(erdos.Timestamp(is_top=True)) open_drive_stream.send(top_msg) global_trajectory_stream.send(top_msg) for stream in streams_to_send_top_on: stream.send(top_msg) time_to_sleep = 1.0 / FLAGS.sensor_frequency count = 0 while True: timestamp = erdos.Timestamp(coordinates=[count]) if not FLAGS.obstacle_detection: obstacles_stream.send(ObstaclesMessage(timestamp, [])) obstacles_stream.send(erdos.WatermarkMessage(timestamp)) if not FLAGS.traffic_light_detection: traffic_lights_stream.send(TrafficLightsMessage(timestamp, [])) traffic_lights_stream.send(erdos.WatermarkMessage(timestamp)) if not FLAGS.obstacle_tracking: obstacles_tracking_stream.send( ObstacleTrajectoriesMessage(timestamp, [])) obstacles_tracking_stream.send(erdos.WatermarkMessage(timestamp)) count += 1 if pylot.flags.must_visualize(): import pygame from pygame.locals import K_n 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)) # NOTE: We should offset sleep time by the time it takes to send the # messages. time.sleep(time_to_sleep)
Example #29
Source File: text_utils.py From SynthText with Apache License 2.0 | 4 votes |
def render_multiline(self,font,text): """ renders multiline TEXT on the pygame surface SURF with the font style FONT. A new line in text is denoted by \n, no other characters are escaped. Other forms of white-spaces should be converted to space. returns the updated surface, words and the character bounding boxes. """ # 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, words, bbs
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