Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Keyboard Arrow Keys
#1
How can I user keyboard arrows keys to move the catcher (box in the game) to the right and left?

Thanks.

# Bomb Catcher Game


import sys, random, time, pygame
from pygame.locals import *

def print_text(font, x, y, text, color=(255,255,255)):
    imgText = font.render(text, True, color)
    screen.blit(imgText, (x,y))
    

#main program begins
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Bomb Catching Game")
font1 = pygame.font.Font(None, 24)
pygame.mouse.set_visible(False)
white = 255,255,255
red = 220, 50, 50
yellow = 230,230,50
black = 0,0,0

lives = 3
score = 0
clock_start = 0
game_over = True
mouse_x = mouse_y = 0

pos_x = 300
pos_y = 460

bomb_x = random.randint(0,500)
bomb_y = -50
vel_y = 0.7

#repeating loop
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == MOUSEMOTION:
            mouse_x,mouse_y = event.pos
            move_x,move_y = event.rel
        elif event.type == MOUSEBUTTONUP:
            if game_over:
                game_over = False
                lives = 3
                score = 0

    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        sys.exit()

    screen.fill((0,0,100))

    if game_over:
        print_text(font1, 100, 200, "<CLICK TO PLAY>") 
    else:
        #move the bomb
        bomb_y += vel_y

        #has the player missed the bomb?
        if bomb_y > 500:
            bomb_x = random.randint(0, 500)
            bomb_y = -50
            lives -= 1
            if lives == 0:
                game_over = True

        #see if player has caught the bomb
        elif bomb_y > pos_y:
            if bomb_x > pos_x and bomb_x < pos_x + 120:
                score += 10
                bomb_x = random.randint(0, 500)
                bomb_y = -50
        
        #draw the bomb
        pygame.draw.circle(screen, black, (bomb_x-4,int(bomb_y)-4), 30, 0)
        pygame.draw.circle(screen, yellow, (bomb_x,int(bomb_y)), 30, 0)

        #set basket position
        pos_x = mouse_x
        if pos_x < 0:
            pos_x = 0
        elif pos_x > 500:
            pos_x = 500
        #draw basket
        pygame.draw.rect(screen, black, (pos_x-4,pos_y-4,120,40), 0)
        pygame.draw.rect(screen, red, (pos_x,pos_y,120,40), 0)

    #print # of lives
    print_text(font1, 0, 0, "LIVES: " + str(lives))

    #print score
    print_text(font1, 500, 0, "SCORE: " + str(score))

    
    
    pygame.display.update()
    

    
Reply
#2
Here's something that hopefully helps. It shows the arrow keys, continuous movement by holding a key down, and smooth movement using an fps clock/delta time, as well as clamping the values so you don't fly off the screen.

import pygame as pg

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)


def main(screen, max_x, max_y):
    x, y = 50, 50
    width, height = 50, 50
    clock = pg.time.Clock()

    x_movement = 0
    delta = 0
    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                return
            if event.type == pg.KEYDOWN:
                # start moving left/right if the keys are pressed
                if event.key == pg.K_LEFT:
                    x_movement = -1
                elif event.key == pg.K_RIGHT:
                    x_movement = 1
            elif event.type == pg.KEYUP:
                # stop movement if the key is un-pressed
                if event.key in [pg.K_LEFT, pg.K_RIGHT]:
                    x_movement = 0

        x += (delta * x_movement)
        # clamp x
        if x <= 0:
            x = 0
        elif x >= max_x - width:
            x = max_x - width

        screen.fill(BLACK)
        pg.draw.rect(screen, WHITE, (x, y, width, height), 0)
        pg.display.update()
        delta = clock.tick(60)


if __name__ == "__main__":
    pg.init()
    width, height = 500, 800
    screen = pg.display.set_mode((width, height))
    main(screen, width, height)
Reply
#3
I can move the box with arrow keys but the rest of the code (the one sent) doesn't work.
Reply
#4
What about the code doesn't work? Are there errors?
Reply
#5
I just downloaded vscode to write a c program, I have a full compiler installed, but I don't understand why when typing, it still doesn't work, please help me. both. Click run code again, it will show the line code is already running. drift boss
[Image: 94758]
Reply


Forum Jump:

User Panel Messages

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