Python Forum
[PyGame] Pong game key.event problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Pong game key.event problem
#1
I'm writing a simple pong game with OOP Wall and i have this issue when i move the left and right paddle, they don't move in a continuous way, i have to press t he up down key every time to move them.
Here is the code of main loop:

class MainLoop:
	while True:
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				pygame.quit()
				quit()
		
			if event.type == pygame.KEYDOWN:

				if event.key == pygame.K_w:
					if pad_left.y >= 2:
						pad_left.y -= 10
				elif event.key == pygame.K_s:
					if pad_left.y <= display_height - pad_left.height:
						pad_left.y += 10
				elif event.key == pygame.K_UP:
					if pad_right.y >= 2:
						pad_right.y -= 10
				elif event.key == pygame.K_DOWN:
					if pad_right.y <= display_height - pad_right.height:
						pad_right.y += 10

		if ball.x <= 0 or ball.x >= display_width:
			ball.x = display_middle[0]
			ball.y = display_middle[1]
			ball.speed_x = 5
			ball.speed_y = 5
			if ball.x <= 0:
				pad_right.score += 1
				ball.speed_x = abs(ball.speed_x)
			elif ball.x >= display_width:
				pad_left.score += 1
				ball.speed_x = -ball.speed_x
			
		# pad colission
		if ball.x <= pad_left.width + ball.radius and ball.y >= pad_left.y+10 and \
		ball.y <= pad_left.y+pad_left.height+10:
			if ball.y >= pad_left.y + 20 and ball.y <= pad_left.y + 60:
				ball.speed_x -= 1
				ball.speed_y -= 2
			else:
				ball.speed_x = -5
				ball.speed_y = 5
			ball.speed_x = -ball.speed_x
			
		if ball.x + ball.radius >= pad_right.x and ball.y >= pad_right.y+10 and \
		ball.y <= pad_right.y+pad_left.height+10:
			if ball.y >= pad_right.y + 20 and ball.y <= pad_right.y + 60:
				ball.speed_x += 1
				ball.speed_y += 2
			else:
				ball.speed_x = 5
				ball.speed_y = -5
			ball.speed_x = -ball.speed_x

		display.fill(background) # <~~
	
		drawText(pad_left.score, white, 70, (display_middle[0]-95,0))
		drawText(pad_right.score, white, 70, (display_middle[0]+60,0))
		pad_left.drawPad()
		pad_right.drawPad()
		#pygame.draw.line(display, white, (display_middle[0],0), (display_middle[0], display_height), 2)
		[ pygame.draw.rect(display, white, (display_middle[0], y, 6, 6)) for y in range(6, display_height, 15)]
		ball.drawBall()
		
		# GAME OVER SCREEN
		if pad_left.score == 1 or pad_right.score == 1:
			display.fill(background)
			drawText('GAME OVER', white, 70, ((200, (display_middle[1]//2))))
			if pad_left.score == 1:
				drawText('Player 1 won', white, 40, (display_middle[0]-145, display_middle[1]))
			if pad_right.score == 1:
				drawText('Player 2 won', white, 40, (display_middle[0]-145, display_middle[1]))
			pad_left.score = 0
			pad_right.score = 0
			pygame.display.update()
			pygame.time.wait(1500)
			if pygame.key.get_pressed()[pygame.K_RETURN]:
				pass
		else:
			pygame.display.update()
		
		clock.tick(30)

if __name__ == '__main__': 
	MainLoop()
I'm learning OOP so if it's something I can do to improve on this code I'll be grateful. thank you. Big Grin

Source code: https://pastebin.com/JdCrXSmJ
Reply
#2
Quote:
if event.type == pygame.KEYDOWN:
this checks for a single event, not consistently holding down a key.

Put this not within the event loop but in the main game loop
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] or keys[pygame.K_w]:
    pass
#etc.
Recommended Tutorials:
Reply
#3
(Dec-12-2018, 02:48 PM)metulburr Wrote:
Quote:
if event.type == pygame.KEYDOWN:
this checks for a single event, not consistently holding down a key.

Put this not within the event loop but in the main game loop
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] or keys[pygame.K_w]:
    pass
#etc.

Doh Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with pygame.event.clear qq12346 1 2,105 Oct-05-2023, 08:39 AM
Last Post: patriciainman
Exclamation pong paddles wont move skullkat232 5 2,337 Feb-13-2023, 03:53 PM
Last Post: Vadanane
  Problem with my pong code Than999 3 3,453 Oct-22-2021, 08:50 AM
Last Post: Qanima
  Game “Pong” I have problems with the code BenBach18 2 3,493 Jan-10-2021, 05:16 PM
Last Post: michael1789
  [PyGame] Creating Pong in Pygame Russ_CW 2 2,811 Oct-11-2020, 11:56 AM
Last Post: Russ_CW
  [PyGame] Problem With Entering Game Loop ElevenDecember 3 2,663 Jan-19-2020, 08:25 AM
Last Post: michael1789
  [PyGame] Problem when moving game window michael1789 15 8,779 Jan-17-2020, 01:40 PM
Last Post: metulburr
  Snake Game - obstacle problem Samira 3 5,607 Oct-31-2019, 02:58 PM
Last Post: Samira
  Trying to make a simple pong game. kevindadmun 1 3,904 Aug-05-2019, 06:39 AM
Last Post: kevindadmun
  Smiley Pong Help Jasmineleroy 6 4,717 May-22-2019, 11:36 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020