简体   繁体   中英

I can't get my 'player' to move to move both ways in pygame, how do i do it?

I am making a game and was wondering how to make my 'player' or green piece to move both ways. Whenever I click any of the arrows he moves right because that's all I can figure out. The animation code is on lines 27 and 28. I need the green circle to be able to move right and left 5 pixels every time either of the arrows are clicked. EDIT: I also need the collision to be when it hits anywhere on the red bullets because in my current code the bullets have to hit the exact coordinates of the player.

import pygame
import random
BLACK = (0,0,0)
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
pygame.init()
size = (700,700)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Dodger")
done = False
clock = pygame.time.Clock()

def resetpositions():
    global bulletrect1, bulletrect2, bulletrect3, bulletrect4, bulletrect5, circlerect
    bulletrect1 = pygame.rect.Rect((350, 0, 20, 20))
    bulletrect2 = pygame.rect.Rect((175, 0, 20, 20))
    bulletrect3 = pygame.rect.Rect((525, 0, 20, 20))
    bulletrect4 = pygame.rect.Rect((525, 0, 20, 20))
    bulletrect5 = pygame.rect.Rect((525, 0, 20, 20))

circlerect = pygame.rect.Rect((350, 600, 20, 20))

resetpositions()
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN:
            circlerect.x += 5
    bulletrect1.y += 1
    bulletrect2.y += 2
    bulletrect3.y += 3
    bulletrect4.y += 4
    bulletrect5.y += 5
    screen.fill(BLACK)
    pygame.draw.circle(screen, GREEN, (circlerect.center), 15)
    pygame.draw.circle(screen, RED, (bulletrect1.center), 20)
    pygame.draw.circle(screen, RED, (bulletrect2.center), 20)
    pygame.draw.circle(screen, RED, (bulletrect3.center), 20)
    pygame.draw.circle(screen, RED, (bulletrect4.center), 20)
    pygame.draw.circle(screen, RED, (bulletrect5.center), 20)
    if bulletrect1.y == 800:
        bulletrect1.y = 0
        bulletrect1.x = random.randint(20,680)
    if bulletrect2.y == 800:
        bulletrect2.y = 0
        bulletrect2.x = random.randint(20,680)
    if bulletrect3.y == 800:
        bulletrect3.y = 0
        bulletrect3.x = random.randint(20,680)
    if bulletrect4.y == 800:
        bulletrect4.y = 0
        bulletrect4.x = random.randint(20,680)
    if bulletrect5.y == 800:
        bulletrect5.y = 0
        bulletrect5.x = random.randint(20,680)
    if circlerect.x == 685:
       circlerect.x = 15
    if circlerect.collidelist((bulletrect1, bulletrect2, bulletrect3, bulletrect4, bulletrect5)) == 0:
        screen.fill(BLACK)
        font = pygame.font.SysFont('Calibri',40,True,False)
        text = font.render("GAME OVER",True,RED)
        screen.blit(text,[250,350])
        pygame.display.update()
        pygame.time.delay(3000)
        resetpositions()
    pygame.display.flip()
    clock.tick(300)
pygame.quit()

Lines 30 and 31 are:

if event.type == pygame.KEYDOWN:
            circlerect.x += 5

This code detects when ANY key is pressed down and will subsequently move the circlerect object 5 units to the right (always to the right because it is always +=).

If you want to move both ways you will have to detect more than just KEYDOWN. You have to detect when the key press is left (and subtract 5) and when it is right (and add 5).

As Isaac mentioned, you have to check which key was pressed by the user and move left -= 5 or right += 5 accordingly.

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
        circlerect.x -= 5
    elif event.key == pygame.K_RIGHT:
        circlerect.x += 5

The problem is that this only moves the player once per key press and you'd have to add variables like moving_left to get continuous movement. In your case it would be easier to use pygame.key.get_pressed instead of handling the events in the event loop, because you can use it to check if a key is held down.

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        done = True

# A list of the pressed keys.
keys = pygame.key.get_pressed()
# If the key at index `K_LEFT` is held.
if keys[pygame.K_LEFT]:
    circlerect.x -= 5
elif keys[pygame.K_RIGHT]:
    circlerect.x += 5

Side note: clock.tick(300) is too high. Better limit your game to 30 or 60 fps and increase the speed of the circles.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM