Python Forum
[PyGame] Shapes dont respond to keyboard input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Shapes dont respond to keyboard input
#1
i try to make a break block type of game but the button line doesnt move left or right, i ve been trying for 2 days this is what i made so far
import pygame
from pygame.locals import *
pygame.init()
pygame.display.init()
Red = (255,0,0)
black = (0,0,0)
yellow = (255,255,0)
aqua = (0,255,255)
w_h = [720, 720]
LEFT = 'left'
screen = pygame.display.set_mode((w_h))
pygame.display.set_caption("tetris clone")
screen.fill(Red)
pygame.draw.rect(screen, black, [25, 20, 100, 25])
pygame.draw.rect(screen, black, [245, 700, 150, 25])
draw = pygame.draw.circle(screen, yellow, (300, 400), 15, 0)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.K_LEFT:
            move = event.pos
                LEFT
    pygame.display.flip()
also i how do i make the ball fall and when the ball hits the button line it will bounce at the walls?thanks!
Reply
#2
(Dec-25-2021, 02:38 AM)Mechanicalpixelz Wrote: i try to make a break block type of game but the buttom line doesnt move left or right, i ve been trying for 2 days this is what i made so far
import pygame
from pygame.locals import *
pygame.init()
pygame.display.init()
Red = (255,0,0)
black = (0,0,0)
yellow = (255,255,0)
aqua = (0,255,255)
w_h = [720, 720]
LEFT = 'left'
screen = pygame.display.set_mode((w_h))
pygame.display.set_caption("tetris clone")
screen.fill(Red)
pygame.draw.rect(screen, black, [25, 20, 100, 25])
pygame.draw.rect(screen, black, [245, 700, 150, 25])
draw = pygame.draw.circle(screen, yellow, (300, 400), 15, 0)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.K_LEFT:
            move = event.pos
                LEFT
    pygame.display.flip()
also i how do i make the ball fall and when the ball hits the button line it will bounce at the walls?thanks!
Reply
#3
You animate things in pygame by changing it's co-ordinates, redrawing the background and then redrawing everything in the foreground. This will animate the lower paddle. You can take it from here... Right?
import pygame
from pygame.locals import *
pygame.init()
pygame.display.init()
Red = (255,0,0)
black = (0,0,0)
yellow = (255,255,0)
aqua = (0,255,255)
w_h = [720, 720]
LEFT = 'left'
screen = pygame.display.set_mode((w_h))
pygame.display.set_caption("tetris clone")
screen.fill(Red)
pygame.draw.rect(screen, black, [25, 20, 100, 25])
pygame.draw.rect(screen, black, [245, 700, 150, 25])
draw = pygame.draw.circle(screen, yellow, (300, 400), 15, 0)
rect_x = 340
while True:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			pygame.quit()
	keys = pygame.key.get_pressed ()
    # This is where we change the x co-ordinate
	if keys [pygame.K_LEFT]:
		rect_x -= 1
	elif keys [pygame.K_RIGHT]:
		rect_x += 1
	screen.fill(Red)
	pygame.draw.rect(screen, black, [25, 20, 100, 25])
	draw = pygame.draw.circle(screen, yellow, (300, 400), 15, 0)
    # This is where we change to the updated x co-ordinate 
	pygame.draw.rect(screen, black, [rect_x, 700, 100, 25])
	pygame.display.flip()
Reply
#4
yes thank you! If only i had made an account sooner and asked here!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I dont know where my local variable has gone Help_me_Please 4 3,015 Apr-04-2020, 05:25 AM
Last Post: michael1789

Forum Jump:

User Panel Messages

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