Pygame (two) game programming and explosive, the second bomb - Incident Response

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/suoyue_py/article/details/99172006

From pygame episode we see just a complete interface that moves only, it can not be regarded as a game. For a game, do better, if the players can not only appreciate and participate in the most "boring", which, to look rehabilitated on a code, so that players can participate.
Program renderings:
Here Insert Picture Description
pygame starting QUIT event has been used to exit the program, and now again use KEYDOWN, KEYUP, MOUSEBUTTONDOWN, MOUSEBUTTONUP event uses to perfect the program code, it allows players to operate.
The event list:
Here Insert Picture Description
analysis of the core code to add the complete code image material and packing papers see the end:

SY1:

In the main game loop add KEYDOWN keyboard press events to control the characters move around
K_LEFT: the keyboard arrow button
K_RIGHT: the keyboard arrow right
K_UP: the keyboard arrow keys
K_DOWN: keyboard arrow down key

#键盘控制事件:上下左右移动  
if event.type == KEYDOWN:       
    if event.key == K_LEFT:
        SPEED = [-2,0]
    elif event.key == K_RIGHT:
        SPEED = [2,0]
    elif event.key == K_UP:
        SPEED = [0,-2]
    elif event.key == K_DOWN:
        SPEED = [0,2]

Now the keyboard arrow keys can be used to operate the moving direction of the character image, but can not control the steering, which, SY3 the methods used to control the vertical transfore image, lateral tilt and zoom operations.

SY2:

在游戏主循环添加"MOUSEBUTTONDOWN"和"MOUSEKEYBUTTONUP"来设定鼠标按住时图像角色进行倍速运动,松开鼠标恢复原速(此处若要多次加速可用一个循环操作)

#按住鼠标进行加速运动,松开鼠标恢复原速
if event.type==MOUSEBUTTONDOWN:
    if SPEED[0] != 0:
        SPEED[0] *= 2
    else:
        SPEED[1] *= 2
elif event.type==MOUSEBUTTONUP:
    if SPEED[0] != 0:
        SPEED[0] /= 2
    else:
        SPEED[1] /= 2

由于按下键盘方向键会改变SPEED的参数(可能出现零),对此加速时需要做判断。

SY3:

transfore方法一览表:
Here Insert Picture Description
flip方法:
加载图像后设置图像的翻转情况,pygame.transform.flip(Surface,xbool,ybool) - > Surface 包含三个参数,
参数1:返回surface对象;
参数2:设置是否水平翻转;
参数3:设置是否垂直翻转

pygame首发代码处有备注

ori_role = pygame.image.load("role.png")    #加载原始图像
role = pygame.transform.flip(ori_role,True,False)    #翻转图像

设置左右翻转情况并判断角色的头朝向

face_left = ori_role
face_right = role
walk_head = True       #判断头的朝向

smoothscale Method:
Set the image scaling ratio: ratio = 1.0 was added and amplified in the game loop, the reduced key and its function.
K_EQUALS: keyboard keys upper left + = lower left
K_MINUS: Keyboard upper left - lower left - button
K_SPACE: spacebar
pygame.transform.smoothscale (Surface, (width, height), DestSurface = None) -> Surface three parameters,
parameter 1: data, returns the object surface;
parameter 2: size (width, height);
parameter 3: mode
used here scale methods are also possible, and use the same basic parameters, the display image is enlarged little deviation

#放大、缩小的角色(=\-),空格键恢复原始尺寸
if (event.key == K_EQUALS) or (event.key == K_MINUS) or (event.key == K_SPACE):
    #最大只能:放大一倍,缩小50%
    if event.key == K_EQUALS and ratio < 2.0:
        ratio += 0.1
    if event.key == K_MINUS and ratio > 0.5:
        ratio -= 0.1
    if event.key == K_SPACE:
        ratio = 1.0       
    #转化后的赋值给role
    role = pygame.transform.smoothscale(ori_role,(int(ori_position.width*ratio),int(ori_position.height*ratio)))     #数据,大小(宽度,高度),模式                    
    face_left = role
    face_right = pygame.transform.flip(role,True,False)
    position.size=(int(ori_position.width*ratio), int(ori_position.height*ratio))   #改变rect对象尺寸使其不出界

Note: After completion of the image enlargement and reduction process needs further details about the: head towards the image of the character in the enlarged or reduced image is generated automatically flipped, this needs to be set and the first direction is determined accordingly, full details see below Code:

import pygame       
from pygame.locals import*      
from sys import exit

pygame.init()       #初始化pygame
pygame.display.set_caption("锁钥 の Hello World !!!")       #设置窗口标题

width,height = 640,480    
SPEED = [2,1]      #定义速度
background_image = 'bg.jpg'   #指定背景图像文件名称

screen = pygame.display.set_mode((width,height),0,32)       #创建窗口
background = pygame.image.load(background_image).convert()     #加载背景图

ori_role = pygame.image.load("role.png")    #加载原始图像
role = pygame.transform.flip(ori_role,True,False)    #翻转图像
ori_position = position = ori_role.get_rect()       #获得图像的原始位置矩形和位置矩形

face_left = ori_role
face_right = role
walk_head = True       #判断头的朝向
ratio = 1.0         #设置图像放大缩小的比率

#游戏主循环
while True:     
    for event in pygame.event.get():
        #接收到退出事件后退出程序
        if event.type == pygame.QUIT:      
            exit()

        #键盘控制事件:上下左右移动  
        if event.type == KEYDOWN:       
            if event.key == K_LEFT:
                role = face_left
                SPEED = [-2,0]
            elif event.key == K_RIGHT:
                role = face_right
                SPEED = [2,0]
            elif event.key == K_UP:
                SPEED = [0,-2]
            elif event.key == K_DOWN:
                SPEED = [0,2]
            
            #放大、缩小的角色(=\-),空格键恢复原始尺寸
            if (event.key == K_EQUALS) or (event.key == K_MINUS) or (event.key == K_SPACE):
                #最大只能:放大一倍,缩小50%
                if event.key == K_EQUALS and ratio < 2.0:
                    ratio += 0.1
                if event.key == K_MINUS and ratio > 0.5:
                    ratio -= 0.1
                if event.key == K_SPACE:
                    ratio = 1.0
                    
                #转化后的赋值给role
                role = pygame.transform.smoothscale(ori_role,(int(ori_position.width*ratio),int(ori_position.height*ratio)))     #数据,大小(宽度,高度),模式                    
                face_left = role
                face_right = pygame.transform.flip(role,True,False)
                position.size=(int(ori_position.width*ratio), int(ori_position.height*ratio))   #改变rect对象尺寸使其不出界

                if walk_head:
                    role = face_right
                else:
                    role = face_left
        
        #按住鼠标进行加速运动,松开鼠标恢复原速
        if event.type==MOUSEBUTTONDOWN:
            if SPEED[0] != 0:
                SPEED[0] *= 2
            else:
                SPEED[1] *= 2
        elif event.type==MOUSEBUTTONUP:
            if SPEED[0] != 0:
                SPEED[0] /= 2
            else:
                SPEED[1] /= 2
            
    #判断运动方向
    if SPEED[0]<0:
        walk_head = False
    if SPEED[0]>0:
        walk_head = True

    #将图像限制在屏幕中运动
    if position.left<0 or position.right>width:     #左右边界判断
        role = pygame.transform.flip(role,True,False) 
        SPEED[0] = -SPEED[0]    #反方向移动
    if position.top<0 or position.bottom>height:    #上下边界判断
        SPEED[1] = -SPEED[1]
        
    position = position.move(SPEED)     #移动图像        
    screen.blit(background,(0,0))   #将背景图画上
    screen.blit(role,position)      #更新图像
    pygame.display.flip()    #更新界面
    pygame.time.delay(5)   #延迟10毫秒

Please pay attention to timely update micro letter public, " Barry lock and key ," No public reply back " pygame second bomb ," fetch the source code, executable file pictures and packaging

Attach the front of knowledge:
into the pit Python
Pygame first bomb

Guess you like

Origin blog.csdn.net/suoyue_py/article/details/99172006