python弹球游戏 第二章、让小球动起来,让木板动起来 - Go语言中文社区

python弹球游戏 第二章、让小球动起来,让木板动起来


运动和交互的实现

1)让小球动起来无外乎需要小球的球心位置不断改变,这就需要在小球对象里面添加一个更新小球位置的函数update

2)让小球动起来需要知道每次改变小球位置的速度量,因此需要初始化小球的速度

3)小球动起来不能让小球的跑出界面外,因此需要有一个修改小球速度的过程,比如碰到右壁应该使得x方向的速度为负

4)木板动起来需要外设输入的响应,基本设置为w,s控制左板上下,上下键控制右板上下

5)木板的移动和小球一样,只是运动的维数降低。当然这里为了使得按住不放生效,需要多定义两个bool量,反映上下移动的许可

6)因为游戏退出其实也是外设的输入导致的,因此可以集成到一个函数里面去检查外设输入事件

五个模块中代码的更新

game_main.py不变:

import pygame
from game_settings import Settings
from board import Board
from ball import Ball
import game_function as gf
pygame.init()#初始化
settings=Settings()#获取设置参数
screen=pygame.display.set_mode((settings.screen_width,settings.screen_height))#创建一个界面,限制长宽
screen.fill(settings.screen_color)#界面上色
pygame.display.set_caption(settings.screen_text)#界面上文字
bd1=Board(screen,settings,'left')#定义左板,定义只需要一次
bd2=Board(screen,settings,'right')#定义右板
ball=Ball(screen,settings)#定义球
while True:
    #gf.check_event(bd1)
    gf.update_screen(settings,screen,ball,bd1,bd2)


game_settings.py添加速度相关量

class Settings():
    def __init__(self):
        self.setting_screen()
        self.setting_board()
        self.setting_ball()
        
    
    def setting_screen(self):#设置界面
        self.screen_color=[0,250,250]
        self.screen_width=420
        self.screen_height=600
        self.screen_text='弹球游戏'
    
    def setting_board(self):#设置板子
        self.board_height=100
        self.board_width=5
        self.board_color=[250,0,250]
        self.board_location=int(self.screen_height/2-self.board_height/2)
        self.board_speed=0.6#只有一个维度运动
        self.board_increase_speed=1.1#木板灵敏度增加系数
    
    def setting_ball(self):#设置弹球参数
        self.ball_color=[253,122,70]
        self.ball_rad=10
        self.ball_width=3
        self.ball_speedx=0.3#运动速度
        self.ball_speedy=0.3
        self.ball_increase_speed=1.01#每次碰撞后加速因子


board.py添加一个函数和运动相关变量

import pygame
class Board():
    def __init__(self,screen,settings,option):
        self.screen=screen
        self.settings=settings
        self.option=option
        if option=='left':#如果创建左板,那么左边距为0
            self.left=0
        if option=='right':#如果创建右板,左边距为如下
            self.left=screen.get_rect().right-self.settings.board_width
        self.color=settings.board_color#board具有四个参数,游戏进程中可能会改变的量
        self.y=settings.board_location
        self.width=settings.board_width
        self.height=settings.board_height
        self.speed=settings.board_speed
        self.moving_up=False#初始两个方向都不许移动,只有在按下按键才移动
        self.moving_down=False
        self.prey=float(self.y)#储存y方向像素距离的浮点值,因为速度可以是小数,所以该值需要小数
    
    def draw_board(self):
        pygame.draw.rect(self.screen,self.color,[self.left,self.y,self.width,self.height])#画板子
    
    def update(self):
        if self.moving_up and self.y>self.screen.get_rect().top:
            self.prey-=self.speed
        if self.moving_down and self.y<self.screen.get_rect().bottom-self.height:
            self.prey+=self.speed
        self.y=int(self.prey)


ball.py添加两个函数和运动相关量

class Ball():
    def __init__(self,screen,settings):
        self.screen=screen
        self.rad=settings.ball_rad#球相关的参数,游戏进程中可能会有所改变
        self.color=settings.ball_color
        self.width=settings.ball_width
        self.centerx=int(screen.get_rect().width/2)
        self.centery=int(screen.get_rect().height/2)
        self.prex=float(self.centerx)#因为后面速度可能是小数,因此需要一个变量储存精确的位置信息
        self.prey=float(self.centery)
        self.xspeed=settings.ball_speedx
        self.yspeed=settings.ball_speedy
        self.increase=settings.ball_increase_speed        
        self.screen_left=screen.get_rect().left
        self.screen_right=screen.get_rect().right
        self.screen_top=screen.get_rect().top
        self.screen_bottom=screen.get_rect().bottom
    
    def draw_ball(self):
        pygame.draw.circle(self.screen,self.color,[self.centerx,self.centery],self.rad,self.width)
    
    def update(self):
        self.update_speed()#更新速度
        self.prex+=self.xspeed#精确值
        self.prey+=self.yspeed
        self.centerx=int(self.prex)#转换为可在屏幕上显示的整点值
        self.centery=int(self.prey)
    
    def update_speed(self):#更新速度
        if self.centerx>self.screen_right-self.rad:#达到最右边
            self.xspeed=-abs(self.xspeed)
        if self.centerx<self.screen_left+self.rad:#达到最左边
            self.xspeed=abs(self.xspeed)
        if self.centery>self.screen_bottom-self.rad:#达到最下边
            self.yspeed=-abs(self.yspeed)
        if self.centery<self.screen_top+self.rad:#达到最上边
            self.yspeed=abs(self.yspeed)


game_function.py添加外设事件处理函数,改动较大

import sys
import pygame
def update_screen(settings,screen,ball,boardl,boardr):#游戏第一个函数,更新界面
    screen.fill(settings.screen_color)#在screen中填充一定颜色
    check_event(boardl,boardr)
    boardl.update()
    boardr.update()
    ball.update()
    boardl.draw_board()#画左板
    boardr.draw_board()#画右板
    ball.draw_ball()#画圆形
    pygame.display.flip()#更新所有display对象

def check_event(bd1,bd2):#所有外端输入都在这个函数中响应
    for event in pygame.event.get():
        if event.type==pygame.QUIT:#点击'x'离开
            sys.exit()
        if event.type==pygame.KEYDOWN:#按下键盘
            response_keydown(event,bd1,bd2)
        if event.type==pygame.KEYUP:#松开键盘
            response_keyup(event,bd1,bd2)

def response_keydown(event,bd1,bd2):#对按下键盘作出响应
    if event.key==pygame.K_w:#w对应允许bd1上移
        bd1.moving_up=True
    if event.key==pygame.K_s:#s对应允许bd1下移
        bd1.moving_down=True
    if event.key==pygame.K_UP:#上键对应bd2上移
        bd2.moving_up=True
    if event.key==pygame.K_DOWN:#下键对应bd2下移
        bd2.moving_down=True
    if event.key==pygame.K_ESCAPE:#按下ESC退出游戏
        sys.exit()

def response_keyup(event,bd1,bd2):#松开按键的响应,和按下对应相反
    if event.key==pygame.K_w:
        bd1.moving_up=False
    if event.key==pygame.K_s:
        bd1.moving_down=False
    if event.key==pygame.K_UP:
        bd2.moving_up=False
    if event.key==pygame.K_DOWN:
        bd2.moving_down=False
老样子,放在一个文件夹中,运行主函数game_main.py,就可以看到一个弹来弹去的球和可以上下移动的板子了


版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_42451864/article/details/80713212
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 18:32:02
  • 阅读 ( 1900 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢