How to use the pygame.event.get 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 bitcraft / pyscroll / tests / demo-2x.py View on Github external
def handle_input(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                self.running = False
                break

            elif event.type == KEYDOWN:
                if event.key == K_UP:
                    self.camera_vector[1] -= 100
                elif event.key == K_DOWN:
                    self.camera_vector[1] += 100
                elif event.key == K_LEFT:
                    self.camera_vector[0] -= 100
                elif event.key == K_RIGHT:
                    self.camera_vector[0] += 100
                elif event.key == K_ESCAPE:
                    self.running = False
                    break
github jrmhaig / Code_Club_Resources / SpaceInvaders / pygame_worksheet / snippets / classes.py View on Github external
def draw(self):                                     # New
        screen.blit(self.image, (self.x, self.y))       # New

# Images
ship_image = pygame.image.load('icons/ship.png')

# Game objects                                          # Changed
ship = GamePiece(150, 260, ship_image)                  # Changed

run = True

while run:
    screen.fill(WHITE)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.KEYDOWN:
            # A key has been pressed
            if event.key == pygame.K_LEFT:
                # Start moving the ship left
                ship.move(LEFT)                         # Changed
            elif event.key == pygame.K_RIGHT:
                # Start moving the ship right
                ship.move(RIGHT)                        # Changed

    # Put the ship on the screen                        # Changed
    ship.draw()                                         # Changed

    # Refresh the screen
    pygame.display.update()
github windelbouwman / ppci-mirror / examples / wasm / wasmboy.py View on Github external
wasm_boy.exports.config(
    False, False, False, False, False, False, False, False, False
)

# Run pygame loop:
pygame.mixer.pre_init(48000)
pygame.init()
print('audio settings:', pygame.mixer.get_init())
resolution = (160, 144)
screen = pygame.display.set_mode(resolution)
channel = pygame.mixer.find_channel()
clock = pygame.time.Clock()

while True:
    # Handle some events:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()

    # Run emulator wasm:
    # 1. Check audio:
    num_samples = wasm_boy.exports.getNumberOfSamplesInAudioBuffer()
    if num_samples > 4096:
        audio_buffer_location = wasm_boy.exports.AUDIO_BUFFER_LOCATION.read()
        audio_data = wasm_boy.exports.memory[
            audio_buffer_location:audio_buffer_location+num_samples*2]
        wasm_boy.exports.clearAudioBuffer()

        # Do some random conversions on the audio:
        audio = np.array(
            [(s - 127)*250 for s in audio_data],
            dtype=np.int16
github CCareaga / Pygame-Examples / Particle System / example.py View on Github external
def eventLoop(self):
		for event in pygame.event.get():
			if not self.hide:
			    self.GUI.check_events(event)
			if event.type == QUIT:
				self.running = 0
				pygame.quit()
			elif event.type == KEYDOWN:
				if event.key == 32:
					if self.hide:
						self.hide = False
					else:
						self.hide = True
			if (event.type == MOUSEBUTTONDOWN and not self.constant) or  (event.type == MOUSEMOTION and self.constant):
				self.createParticles()
github diku-dk / futhark-benchmarks / rodinia / hotspot / hotspot-gui.py View on Github external
def react_temp(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                raise HotSpotQuit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    self.state = DEFINING_POWER
                    return
github horstjens / ThePythonGameBook / pygame / 013_catch_the_thief_dirtyrect.py View on Github external
background.blit(write("the cross is always in the middle between snake and bird"), (10,50))
    background.blit(write("the blue circle (police) moves toward the cross"),(10,70))
    background.blit(write("catch the red triangle with the blue circle to win points"),(10,90))
    screen.blit(background, (0,0))     # blit background on screen (overwriting all)
    clock = pygame.time.Clock()        # create pygame clock object 
    mainloop = True
    FPS = 60                     # desired max. framerate in frames per second.       
    playtime = 60.0              # seconds of playtime left
    points = 0.0
    gameOver = False
    gameOverSound = True
    while mainloop:
        milliseconds = clock.tick(FPS)  # milliseconds passed since last frame
        seconds = milliseconds / 1000.0 # seconds passed since last frame
        playtime -= seconds
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                mainloop = False # pygame window closed by user
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    mainloop = False # user pressed ESC
        pygame.display.set_caption("[FPS]: %.2f Snake: dx %i dy %i Bird:"
                                   " dx %i dy %i police: dx %.2f dy %.2f " % 
                                   (clock.get_fps(), snakedx, snakedy,
                                    birddx, birddy, policedx, policedy ))
        if playtime < 0:
            gameOver = True
        if gameOver:
            #background.fill((255,255,255)) # white b
            screen.blit(background,(0,0))
            if gameOverSound:
                over.play()
github JasonCromer / Personal_Projects / Python Projects / Python Ping Pong / MyPong.py View on Github external
SCR_WID, SCR_HEI = 640, 480
screen = pygame.display.set_mode((SCR_WID, SCR_HEI))
pygame.display.set_caption("Pong")
pygame.font.init()
clock = pygame.time.Clock()
FPS = 60

ball = Ball()
player = Player()
enemy = Enemy()
ai = AI()

while True:
    #process
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            print("Game exited by user")
            exit()
    #process
    #logic
    ball.movement()
    player.movement()
    ai.movement()
    #logic
    #draw
    screen.fill((0,0,0))
    ball.draw()
    player.draw()
    player.scoring()
    ai.draw()
    enemy.scoring()
github ddorn / GUI / GUI / vracabulous.py View on Github external
def update(self):
        """Get all events and process them by calling update_on_event()"""
        events = pygame.event.get()
        for e in events:
            self.update_on_event(e)

        for wid, cond in self._widgets:
            if cond():
                wid.update(events)
github Schrolli91 / BOSWatch / exampleAddOns / alarmMonitorRPi / displayServices.py View on Github external
logging.debug("eventHandler-thread started")
	
	try:
		clock = pygame.time.Clock()

		# Running will be set to False if main program is shutting down
		while globals.running == True:
			# This limits the while loop to a max of 2 times per second.
			# Leave this out and we will use all CPU we can.
			clock.tick(2)
			
			# current time for this loop:
			curtime = int(time.time())

			for event in pygame.event.get():
				# event-handler for QUIT
				if event.type == pygame.QUIT: 
					globals.running = False

				# if touchscreen pressed
				if event.type == pygame.MOUSEBUTTONDOWN:
					(posX, posY) = (pygame.mouse.get_pos() [0], pygame.mouse.get_pos() [1])
					#logging.debug("position: (%s, %s)", posX, posY)

					# touching the screen will stop alarmSound in every case
					pygame.mixer.stop()
					
					# touching the dark display will turn it on for n sec
					if globals.showDisplay == False:
						logging.info("turn ON display")
						globals.enableDisplayUntil = curtime + globals.config.getint("AlarmMonitor","showDisplayTime")
github horstjens / ThePythonGameBook / pygame / template006_reflect_and_auto-aim.py View on Github external
def run(self):
        """The mainloop"""
        self.create_world() 
        running = True
        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False 
                elif event.type == pygame.KEYDOWN: # press and release
                    if event.key == pygame.K_ESCAPE:
                        running = False
                    if event.key == pygame.K_SPACE: # fire forward from tux1 with 300 speed
                        Bullet(radius=5, x=self.tux1.x, y=self.tux1.y,
                               dx=-math.sin(self.tux1.angle*GRAD)*300,
                               dy=-math.cos(self.tux1.angle*GRAD)*300)           
                    if event.key == pygame.K_w:
                        self.tux1.y -= 50
                        self.tux1.angle = 0
                    if event.key == pygame.K_s:
                        self.tux1.y += 50
                        self.tux1.angle = 180
                    if event.key == pygame.K_a: