How to use the pygame.event function in pygame

To help you get started, we’ve selected a few pygame examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Begintocodewithpython / samples / Ch 04 Working with Data in Python / snaps.py View on Github external
def get_key():
    '''
    Waits for a keypress and then returns it as a string
    Only characters are returned, not control keys
    '''
    setup()
    while True:
        event = pygame.event.wait()
        if event.type == 2:
            # Event 2 is keydown
            key_code = event.dict['unicode']
            if key_code:
                return key_code
github zork9 / pygame-pyZeldaII / roadsign.py View on Github external
def talk(self, screen,font):
        if self.talkcounter == 0:
            return
        
        if self.talkcounter > 1:
                self.talkcounter = 0
                return 
        talkover = 0
        while talkover == 0:
            pygame.display.update()
            if self.talkcounter == 1:
                screen.blit(font.render("", 6, (255,255,255)), (10,100))
            for event in pygame.event.get():
                if event.type == QUIT:
                    return
                elif event.type == KEYDOWN:
                    if event.key == K_z:
                        talkover = 1
                        self.talkcounter += 1
github cheery / essence / essence2 / graphics / __init__.py View on Github external
def eventloop(emit=stub, width=640, height=480):
    global screen
    pygame.display.init()
    screen = Surface(pygame.display.set_mode((width, height), pygame.RESIZABLE))
    while 1:
        for event in pygame.event.get():
            dispatch(emit, event)
        emit('frame', [screen])
        pygame.display.flip()
github brydon-zz / pyWiiKinectSwype / lib / swype / mouseTracker.py View on Github external
pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEMOTION and recording == True:
            linesToDraw.append(event.pos)
            trajectory.AddFrame(event.pos) 
            screen.blit(img, (0,0))
            pygame.draw.lines(screen, (255, 127, 80), False, linesToDraw, 4)
            pygame.display.update()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            linesToDraw = [event.pos]
            if recording:
                stime = time.time()
                analysis.AnalyzeTrajectory(trajectory)
                print time.time()-stime
                while True:
                    event = pygame.event.poll()
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        frameData = FrameData(event.pos)
                        if frameData.getClosestLetter() == 's':
                            trajectory.Pickle()
                        break
                        #     break
                        # elif frameData.getClosestLetter() == 'c':
                        #     break
                    elif event.type == pygame.KEYDOWN:
                        trajectory.word += chr(event.key)
                trajectory = SwypeTrajectory("training/trajectory" + re.sub(r' ', r'_', str(datetime.datetime.now())))
            recording = not recording
                

        screen.fill((0, 0, 0))
        # pygame.display.flip()
github ragnraok / RandomMaze / main.py View on Github external
screen = pygame.display.set_mode(resolution, 0, 32)
        clock = pygame.time.Clock()
        maze, cell_list = maze_method()

        path_finder = MazePathFinder(maze, (2, 1), (cell_row_num * 2, cell_col_num * 2 + 1), 
                              height//grid_size, width//grid_size)
        path = path_finder.bfs_find_path()

        index = 0
        path_index = 0
        maze_finished = False

        pass_time = 0
        grid_view = GridView(screen, width, height, grid_size, grid_line_color) 
        while True:
                for event in pygame.event.get():
                        if event.type == QUIT:
                                exit()

                press_key = pygame.key.get_pressed()
                
                # press F5 to regenerate the maze
                if press_key[K_F5]:
                        index = 0
                        path_index = 0
                        maze, cell_list = maze_method()
                        path_finder = MazePathFinder(maze, (2, 1), (cell_row_num * 2, cell_col_num * 2 + 1), 
                                height//grid_size, width//grid_size)
                        path = path_finder.bfs_find_path()
                        maze_finished = False

                #print index
github singularity / singularity / singularity / code / graphics / dialog.py View on Github external
def fake_click(down):
    if down:
        type = pygame.MOUSEBUTTONDOWN
    else:
        type = pygame.MOUSEBUTTONUP
    click_event = pygame.event.Event(type, {'button': 1, 'pos': pygame.mouse.get_pos()})
    pygame.event.post(click_event)
github venthur / pyff / src / Feedbacks / Oddball / P300_Rectangle.py View on Github external
def prompt_count(self):
        self.countrow = Textrow(text="", pos=self.screen.get_rect().center,textsize=60, color=(150, 150, 255), size=(100, 60), edgecolor=(255, 255, 255), antialias=True, colorkey=(0, 0, 0))
        pygame.event.clear()
        text, ready = "", False
        while not ready:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    k = event.key
                    if k == pygame.K_BACKSPACE:
                        if len(text) > 0: text = text[0: - 1]   # Delete last number
                    elif len(text) < 2:
                        if k in (pygame.K_0,pygame.K_KP0): text = text + "0"
                        elif k in (pygame.K_1,pygame.K_KP1): text = text + "1"
                        elif k in (pygame.K_2,pygame.K_KP2): text = text + "2"
                        elif k in (pygame.K_3,pygame.K_KP3): text = text + "3"
                        elif k in (pygame.K_4,pygame.K_KP4): text = text + "4"
                        elif k in (pygame.K_5,pygame.K_KP5): text = text + "5"
                        elif k in (pygame.K_6,pygame.K_KP6): text = text + "6"
                        elif k in (pygame.K_7,pygame.K_KP7): text = text + "7"
                        elif k in (pygame.K_8,pygame.K_KP8): text = text + "8"
                        elif k in (pygame.K_9,pygame.K_KP9): text = text + "9"
                    elif k == pygame.K_RETURN: ready = True
github techwithtim / Agar-IO / game.py View on Github external
player["x"] = player["x"] + vel

		if keys[pygame.K_UP] or keys[pygame.K_w]:
			if player["y"] - vel - PLAYER_RADIUS - player["score"] >= 0:
				player["y"] = player["y"] - vel

		if keys[pygame.K_DOWN] or keys[pygame.K_s]:
			if player["y"] + vel + PLAYER_RADIUS + player["score"] <= H:
				player["y"] = player["y"] + vel

		data = "move " + str(player["x"]) + " " + str(player["y"])

		# send data to server and recieve back all players information
		balls, players, game_time = server.send(data)

		for event in pygame.event.get():
			# if user hits red x button close window
			if event.type == pygame.QUIT:
				run = False

			if event.type == pygame.KEYDOWN:
				# if user hits a escape key close program
				if event.key == pygame.K_ESCAPE:
					run = False


		# redraw window then update the frame
		redraw_window(players, balls, game_time, player["score"])
		pygame.display.update()


	server.disconnect()
github YannThorimbert / Thorpy / thorpy / elements / ddlf.py View on Github external
def _ddlf_reaction_press(self, event):
        x, y = self._get_dirviewer_coords(event.pos)
        lift = False
        if self._lift:
            lift = self._lift.collide(event.pos)
        if x < self._dv.size[0] and not lift:
            self._clicked = self._dv.get_txt_at_pix(x, y)
            if self._clicked:
                if self._menu:
                    self._menu.set_leave()
                else:
                    event_ddl = pygevent.Event(constants.THORPY_EVENT,
                                            id=constants.EVENT_DDL,
                                            el=self,
                                            value=self._clicked)
                    pygevent.post(event_ddl)
                    return self._clicked
github viblo / pymunk / examples / pymunx_demo6_throw_and_settings.py View on Github external
screen.fill((255,255,255))

        # Update & Draw World
        world.update()
        world.draw(screen)

        # Show line if drawing a wall
        if len(points) > 1:
            if draw_poly:
                pygame.draw.lines(screen, THECOLORS["black"], False, points)
            elif draw_arrow:
                blit_arrow(screen, points[0], points[-1])
        
        # For holding Up/Down, call often
        if key_up:
            pygame.event.post(pygame.event.Event(MOUSEBUTTONDOWN, {'button' : 4}))
        elif key_down:
            pygame.event.post(pygame.event.Event(MOUSEBUTTONDOWN, {'button' : 5}))

        # Flip Display
        pygame.display.flip()
        
        # Try to stay at 50 FPS
        clock.tick(50)
        
        # output framerate in caption
        pygame.display.set_caption("elements: %i | fps: %s" % (world.get_element_count(), str(int(clock.get_fps()))))