1

I'm trying to make the paddles move up and down : 1 rectangle. moving with w/a/s/d keys 2 rectangle. with arrows. I'm having trouble making the paddles stay in the screen?

You will make two “paddles” (i.e. Rectangles), one on the left side and one on the right side of the screen. These rectangles should be taller than they are wide (i.e. Look like a paddle from a pong game). The paddle on the left should be able to move up and down using the w and s keys, the paddle on the right should move up and down using the up and down arrow keys. Both paddles should not be allowed to leave the top or bottom of the screen. (* for this assignment you are just creating the paddles and getting them moving properly,, no balls necessary). Try to avoid hardcoded values.

# import the necessary modules
import pygame
import sys

#initialize pygame
pygame.init()

# set the size for the surface (screen)
screen_h = 800
screen_w = 600

screen = pygame.display.set_mode((screen_h,screen_w),0)

# set the caption for the screen
pygame.display.set_caption("Template")

# define colours you will be using
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
YELLOW = (255,255,0)

#initialize variables for player
#variables for first rectangle
R1x = 740
R1y = 300
R1w = 50
R1h = 132
R1dx = 0
R1dy = 0

#variables for second rectangle
R2x = 10
R2y = 300
R2w = 50
R2h = 132
R2dx = 0
R2dy = 0

#speed
speed = 3

screen.fill(BLACK)

# set main loop to True so it will run
main = True
# main loop
while main:
    for event in pygame.event.get(): # check for any events (i.e key press, mouse click etc.)
        if event.type ==pygame.QUIT: # check to see if it was "x" at top right of screen
            main = False         # set the "main" variable to False to exit while loop
        if event.type ==pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                R1dx = 0
                R1dy = -speed
            elif event.key == pygame.K_DOWN:
                R1dx = 0
                R1dy = speed
            if event.key == pygame.K_w:
                R2dx = 0
                R2dy = -speed
            elif event.key == pygame.K_s:
                R2dx = 0
                R2dy = speed
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                R1dx = 0
                R1dy = 0
            elif event.key == pygame.K_w or event.key == pygame.K_s:
                R2dx = 0
                R2dy = 0


    # move the x and y positions of the rectangles
    oldR1x = R1x
    oldR1y = R1y

    R1x = R1x + R1dx
    R1y = R1y + R1dy

    if R1x >= screen_w-50:
        R1x = oldR1x
        R1y = oldR1y
    if R1x<= 50:
        R1x = oldR1x
        R1y = oldR1y
    if R1y>= screen_h-50:
        R1x = oldR1x
        R1y = oldR1y
    if R1y<= 50:
        R1x = oldR1x
        R1y = oldR1y

    # draw the shapes, in this case the blue rectangles
    pygame.draw.rect(screen, WHITE,(R1x, R1y, R1w, R1h),0)
    pygame.draw.rect(screen, WHITE,(R2x, R2y, R2w, R2h),0)

    # we are using .flip() here,  it basically works the same as .update()
    # we will discuss this more in class (you can use either one)
    pygame.display.flip()

# quit pygame and exit the program (i.e. close everything down)
pygame.quit()
sys.exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
i'm sorry
  • 53
  • 5

1 Answers1

1

First of all you have swapped the screen width (screen_w) and the screen height (screen_h). It has to be:

# set the size for the surface (screen)
screen_w = 800
screen_h = 600

screen = pygame.display.set_mode((screen_w,screen_h),0)

The paddles move just up an down, so it sufficient to limit the y coordinate of the pddels to the range [0, screen_h-R1h]. Note, R1y respectively R2y are the top coordinate of the paddles:

R1y = max(0, min(screen_h-R1h, R1y + R1dy))
R2y = max(0, min(screen_h-R2h, R2y + R2dy))

Furthermore, the screen has to be cleared in the main application loop (screen.fill(BLACK)):

while main:
    for event in pygame.event.get(): # check for any events (i.e key press, mouse click etc.)
        if event.type ==pygame.QUIT: # check to see if it was "x" at top right of screen
            main = False         # set the "main" variable to False to exit while loop
        if event.type ==pygame.KEYDOWN:
            # [...]

    # move the x and y positions of the rectangles
    R1y = max(0, min(screen_h-R1h, R1y + R1dy))
    R2y = max(0, min(screen_h-R2h, R2y + R2dy))

    # clear screen
    screen.fill(BLACK)

    # draw the shapes, in this case the blue rectangles
    pygame.draw.rect(screen, WHITE,(R1x, R1y, R1w, R1h),0)
    pygame.draw.rect(screen, WHITE,(R2x, R2y, R2w, R2h),0)

    # we are using .flip() here,  it basically works the same as .update()
    # we will discuss this more in class (you can use either one)
    pygame.display.flip()

Note, it the paddles would move along the x axis too (left, right), then the restriction of the x coordinate is similar:

R1x = max(0, min(screen_w-R1w, R1x + R1dx))
R2x = max(0, min(screen_w-R2w, R2x + R2dx))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thank you but now there's a a continuous trail is left behind. How would I be able to make it doing everything it is doing without leaving the trail – i'm sorry Mar 01 '20 at 18:52
  • the paddles are leaving a trail behind..I want it to be like pong without the ball. the paddle needs to just be moving up and down without the trail!... – i'm sorry Mar 01 '20 at 19:15
  • He even just pasted the assignment text here :D – bronlund Mar 01 '20 at 19:26