2

I have this code:

plaxer = 20
player = 300
vel = 5
res = (720,720)
thistle = (216,191,216)
plum = (221,160,221)
screen = pygame.display.set_mode(res) 
color = (255,255,255) 
color_light = (255,0,0) 
color_dark = (200,0,0) 
red = (128, 0, 128) 
reder = (31,0,31) 
    
width = screen.get_width() 
height = screen.get_height() 
      
smallfont = pygame.font.SysFont('Corbel',35) 
text = smallfont.render('Exit ' , True , color) 
small = pygame.font.SysFont('Corbel',22) 
texta = small.render('Customise Avatar ' , True , color)
screen.fill((0,0,255)) 
img1 = pygame.image.load("C:\\Users\\Path\\To\\Brown.png")
screen.blit(img1,(plaxer, player))
runs = True
    
while runs: 
    mouse = pygame.mouse.get_pos()
    for ev in pygame.event.get():
        keys = pygame.key.get_pressed()  
        if keys[pygame.K_a]:
            plaxer =+ 0
            screen.fill((0,0,255))
            screen.blit(img1, (plaxer, player))

The problem is that the sprite doesn't move properly - it moves too robotically. How do I fix this?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • You've mixed up your input methods. You only handle keys in the event loop (``for ev in pygame.event.get()``). You should move that out of the loop. You should also create a ``pygame.time.Clock`` and use it to regulate framerate. – Starbuck5 Mar 23 '21 at 07:36
  • I edited your post to remove personal information, make the indentation consistent, and [remove noise](https://meta.stackoverflow.com/questions/260776/should-i-remove-fluff-when-editing-questions). However, I don't understand what you mean by "too robotically". – Karl Knechtel Mar 23 '21 at 08:07
  • try to make a max force and max speed variable and try to implement that to your charachter so that the player can have a bit of realistic movement. – Summon Mar 23 '21 at 16:37

1 Answers1

1

You have to draw the player and update the display in the application loop:

while runs: 
    mouse = pygame.mouse.get_pos()
    for ev in pygame.event.get():
        if ev.type == pygame.QUIT:
            runs = False

    keys = pygame.key.get_pressed()  
    if keys[pygame.K_a]:
        plaxer += 1

    screen.fill((0,0,255))
    screen.blit(img1, (plaxer, player))
    pygame.display.update()

The typical PyGame application loop has to:

Rabbid76
  • 202,892
  • 27
  • 131
  • 174