Any way to refactor this in a neater way? Specifically lines 7-11 in relation to line 39? Using Pygame

Viewed 48

I am teaching this to 5th graders so it needs to be as simple as possible. However, as you see inline 39 I am using stored variables for the function parameters. However is there an easier way? Let's say I wanted to put in a 2nd character I would have to put that long list of variables again?

Should I create a character class? If so, how would I plug that into a parameter?

import pygame       
pygame.init()        

win = pygame.display.set_mode((1000, 1000)) 

#Character Details
x = 100 
y = 5  
width = 10
height = 10
vel = 20 

run = True      
while run:
  pygame.time.delay(100)     

  for event in pygame.event.get():    
    if event.type == pygame.QUIT: 
      run = False

#Movement Details
  keys = pygame.key.get_pressed()

  if keys[pygame.K_LEFT]:     
    x -= vel                  #

  if keys[pygame.K_RIGHT]:
    x += vel

  if keys[pygame.K_UP]:
    y -= vel

  if keys[pygame.K_DOWN]:
    y += vel

#Without this character movement is additive.
  win.fill((0, 0, 0)) 

  pygame.draw.rect(win, (245, 100, 50), (x, y, width, height))

  pygame.display.update()
    ```
2 Answers

You could make the variables a Rect object.

player1 = pygame.Rect(100,5,10,10) #x,y,w,h
player2 = pygame.Rect(200,20,10,10)
...
pygame.draw.rect(win,(245,100,50),player1)
pygame.draw.rect(win,(245,100,50),player2)

you could also use a list and it would work just as fine, but with the Rect, it calculates extra attributes for you like

player1.center #get center of the player, both x and y
player.right #get right side of player, same as x position + width

you can also move using these

player1.center = (200,200) #moves the center to (200,200)

But you would need to have a velocity variable for each player, which you could have a list

@The Big Kahuna has already suggested that you can replace one of the parameters to the pygame.draw.rect() with a rect representing the player. That is specifically answering your question and you should do that.

Since you are teaching this to others I wanted to suggest some other things as well.

The first is a tweak to what @The Big Kahuna said. He correctly suggested that you should use the the pygame rect class, which you should. Instead of passing the four parameters like that, Rect allows you you create rect's by calling it with the position and size parameters grouped together. I.e you can group the two position parameters and the two size parameters. Like this:

size = (10, 10) # width, height
player1 = pygame.Rect((100, 5), size)
player2 = pygame.Rect((200, 20), size)

You could separate the position out as well if desired.

The other thing that you can do (and the real reason that I am actually commenting here) to make it cleaner is to look at the other parameter to pygame.draw.rect(). The (245,100,50) is the color you are drawing (see the docs here). Instead of just using numbers like that, it can be made more clear by assigning that color to a variable somewhere near the top of your program and using the variable in the draw call. Like this:

orangey = (245, 100, 50)
...
pygame.draw.rect(win, orangey, player1)

In this case I called the color orangey, because when I displayed it it looked kind of orangey to me. but obviously you can name it whatever is appropriately descriptive.

You can use multiple colors to distinguish the player rect's like this:

orange = pygame.Color("orange")
purple = pygame.Color("purple")
...
pygame.draw.rect(win, orange, player1)
pygame.draw.rect(win, purple, player2)

You can see a good way to find defined colors like the ones I showed by looking at the answer to the question here.

Related