Introduction to Python Game Programming 3

User Input: The Bomb Catcher Game
This chapter describes using the keyboard and mouse to obtain user input. Topics include:
Learning pygame events
Learning the real-time loop
Learning keyboard and mouse events
Learning to poll the keyboard and mouse state
Writing a Bomb Catcher game

1 pygame events covered in this chapter
QUIT
KEYDOWN
KEYUP
MOUSEMOTION
MOUSEBUTTONUP
MOUSEBUTTONDOWN

    1.1 The real-time event loop
    while True:
        for event in pygame .event.get():
            if event.type==QUIT:
                sys.exit()
    continuously processes specific events in the event queue in a loop.

    1.2 Keyboard events
    KEYUP KEYDOWN
    
    1.3 Mouse events
    MOUSEBUTTONDOWN MOUSEBUTTONUP MOUSEMOTION For
    specific properties, see "Several Important Modules of Pygame"

2 Device Polling
The event system in pygame is not the only method we can use to detect user input. We can poll the input device to see if the user is interacting with our
program.

    2.1 Polling the keyboard
    In pygame, use pygame.key.get_pressed() to poll the keyboard interface. This method returns a list of boolean values ​​corresponding to the keys
    on the keyboard, one flag for each key. Use the same key constant value to index the boolean array. The benefit of polling all keys at once is
    that multiple key presses can be detected without having to go through the event system.
    The following example detects the escape key.
    keys=pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        sys.exit()

    2.2 Polling the mouse
    pygame.mouse.get_pos()
    pygame.mouse.get_rel()
    pygame.mouse.get_pressed()
    For specific use, refer to "pygame" Several important modules"
import sys,pygame
from pygame.locals import *
pygame.init()

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
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Mouse Demo")
font1=pygame.font.Font(None,24)
white=255,255,255

mouse_x=mouse_y=0
move_x=move_y=0
mouse_down=mouse_up=0
mouse_down_x=mouse_down_y=0
mouse_up_x=mouse_up_y=0

#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==MOUSEBUTTONDOWN:
            mouse_down=event.button
            mouse_down_x,mouse_down_y=event.pos
        elif event.type==MOUSEBUTTONUP:
            mouse_up=event.button
            mouse_up_x,mouse_up_y=event.pos

    keys=pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        sys.exit()
    
    screen.fill((0,100,0))
    print_text(font1,0,0,"Mouse Events")
    print_text(font1,0,20,"Mouse position: "+str(mouse_x)+","+str(mouse_y))
    print_text(font1,0,40,"Mouse relative: "+str(move_x)+","+str(move_y))
    print_text(font1,0,60,"Mouse button down: "+str(mouse_down)+" at "+str(mouse_down_x)+","+str(mouse_down_y))
    print_text(font1,0,80,"Mouse button up: "+str(mouse_up)+" at "+str(mouse_up_x)+","+str(mouse_up_y))

    print_text(font1,0,160,"Mouse Polling")
    x,y=pygame.mouse.get_pos()
        print_text(font1,0,180,"Mouse Position: "+str(x)+str(y))
        b1,b2,b3=pygame.mouse.get_pressed()
        print_text(font1, 0,200,"Mouse buttons: "+str(b1)+","+str(b2)+","+str(b3))

    pygame.display.update() 3Game



Introduction The
bomb Catcher game combines mouse input, some Basic graphics drawing and a small amount of conflict detection logic.
Bombs are yellow circles that repeatedly fall from the top of the screen.
When the bomb reaches the bottom of the screen, the player will lose a life without catching the bomb.
When the bomb hits the baffle, it counts as the player catching the bomb, and another bomb will continue to fall.

import sys,random,time,pygame
from pygame.locals import *
pygame.init()

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

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
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=3

#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 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>480:
        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()

can be improved a lot, such as difficulty increase, bomb display effect, etc.
Now I will improve the program, but I can't write this program on my own.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324509151&siteId=291194637