1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 用python的pygame模块制作弹球小游戏

用python的pygame模块制作弹球小游戏

时间:2021-11-16 09:47:53

相关推荐

用python的pygame模块制作弹球小游戏

首先那,需要安装pygame模块,大约300行代码就可以做一个小游戏

命令提示窗口中输入:pip install pygame

如果不安装模块,呵呵,

github下载:

/zouzhuo939/python_-

源代码解析:

# 导入模块import pygamefrom pygame.locals import *import sys, random, time, mathclass GameWindow(object):'''创建游戏窗口类'''def __init__(self, *args, **kw):self.window_length = 600self.window_wide = 500# 绘制游戏窗口,设置窗口尺寸self.game_window = pygame.display.set_mode((self.window_length, self.window_wide))# 设置游戏窗口标题pygame.display.set_caption("CatchBallGame")# 定义游戏窗口背景颜色参数self.window_color = (135, 206, 250)def backgroud(self):# 绘制游戏窗口背景颜色self.game_window.fill(self.window_color)class Ball(object):'''创建球类'''def __init__(self, *args, **kw):# 设置球的半径、颜色、移动速度参数self.ball_color = (255, 215, 0)self.move_x = 1self.move_y = 1self.radius = 100def ballready(self):# 设置球的初始位置、self.ball_x = self.mouse_xself.ball_y = self.window_wide - self.rect_wide - self.radius# 绘制球,设置反弹触发条件pygame.draw.circle(self.game_window, self.ball_color, (self.ball_x, self.ball_y), self.radius)def ballmove(self):# 绘制球,设置反弹触发条件pygame.draw.circle(self.game_window, self.ball_color, (self.ball_x, self.ball_y), self.radius)self.ball_x += self.move_xself.ball_y -= self.move_y# 调用碰撞检测函数self.ball_window()self.ball_rect()# 每接5次球球速增加一倍if self.distance < self.radius:self.frequency += 1if self.frequency == 5:self.frequency = 0self.move_x += self.move_xself.move_y += self.move_yself.point += self.point# 设置游戏失败条件if self.ball_y > 520:self.gameover = self.over_font.render("Game Over", False, (0, 0, 0))self.game_window.blit(self.gameover, (100, 130))self.over_sign = 1class Rect(object):'''创建球拍类'''def __init__(self, *args, **kw):# 设置球拍颜色参数self.rect_color = (255, 0, 0)self.rect_length = 100self.rect_wide = 10def rectmove(self):# 获取鼠标位置参数self.mouse_x, self.mouse_y = pygame.mouse.get_pos()# 绘制球拍,限定横向边界if self.mouse_x >= self.window_length - self.rect_length // 2:self.mouse_x = self.window_length - self.rect_length // 2if self.mouse_x <= self.rect_length // 2:self.mouse_x = self.rect_length // 2pygame.draw.rect(self.game_window, self.rect_color, ((self.mouse_x - self.rect_length // 2), (self.window_wide - self.rect_wide), self.rect_length, self.rect_wide))class Brick(object):def __init__(self, *args, **kw):# 设置砖块颜色参数self.brick_color = (139, 126, 102)self.brick_list = [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1],[1, 1, 1, 1, 1, 1]]self.brick_length = 80self.brick_wide = 20def brickarrange(self):for i in range(5):for j in range(6):self.brick_x = j * (self.brick_length + 24)self.brick_y = i * (self.brick_wide + 20) + 40if self.brick_list[i][j] == 1:# 绘制砖块pygame.draw.rect(self.game_window, self.brick_color,(self.brick_x, self.brick_y, self.brick_length, self.brick_wide))# 调用碰撞检测函数self.ball_brick()if self.distanceb < self.radius:self.brick_list[i][j] = 0self.score += self.point# 设置游戏胜利条件if self.brick_list == [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0]]:self.win = self.win_font.render("You Win", False, (0, 0, 0))self.game_window.blit(self.win, (100, 130))self.win_sign = 1class Score(object):'''创建分数类'''def __init__(self, *args, **kw):# 设置初始分数self.score = 0# 设置分数字体self.score_font = pygame.font.SysFont('arial', 20)# 设置初始加分点数self.point = 1# 设置初始接球次数self.frequency = 0def countscore(self):# 绘制玩家分数my_score = self.score_font.render(str(self.score), False, (255, 255, 255))self.game_window.blit(my_score, (555, 15))class GameOver(object):'''创建游戏结束类'''def __init__(self, *args, **kw):# 设置Game Over字体self.over_font = pygame.font.SysFont('arial', 80)# 定义GameOver标识self.over_sign = 0class Win(object):'''创建游戏胜利类'''def __init__(self, *args, **kw):# 设置You Win字体self.win_font = pygame.font.SysFont('arial', 80)# 定义Win标识self.win_sign = 0class Collision(object):'''碰撞检测类'''# 球与窗口边框的碰撞检测def ball_window(self):if self.ball_x <= self.radius or self.ball_x >= (self.window_length - self.radius):self.move_x = -self.move_xif self.ball_y <= self.radius:self.move_y = -self.move_y# 球与球拍的碰撞检测def ball_rect(self):# 定义碰撞标识self.collision_sign_x = 0self.collision_sign_y = 0if self.ball_x < (self.mouse_x - self.rect_length // 2):self.closestpoint_x = self.mouse_x - self.rect_length // 2self.collision_sign_x = 1elif self.ball_x > (self.mouse_x + self.rect_length // 2):self.closestpoint_x = self.mouse_x + self.rect_length // 2self.collision_sign_x = 2else:self.closestpoint_x = self.ball_xself.collision_sign_x = 3if self.ball_y < (self.window_wide - self.rect_wide):self.closestpoint_y = (self.window_wide - self.rect_wide)self.collision_sign_y = 1elif self.ball_y > self.window_wide:self.closestpoint_y = self.window_wideself.collision_sign_y = 2else:self.closestpoint_y = self.ball_yself.collision_sign_y = 3# 定义球拍到圆心最近点与圆心的距离self.distance = math.sqrt(math.pow(self.closestpoint_x - self.ball_x, 2) + math.pow(self.closestpoint_y - self.ball_y, 2))# 球在球拍上左、上中、上右3种情况的碰撞检测if self.distance < self.radius and self.collision_sign_y == 1 and (self.collision_sign_x == 1 or self.collision_sign_x == 2):if self.collision_sign_x == 1 and self.move_x > 0:self.move_x = - self.move_xself.move_y = - self.move_yif self.collision_sign_x == 1 and self.move_x < 0:self.move_y = - self.move_yif self.collision_sign_x == 2 and self.move_x < 0:self.move_x = - self.move_xself.move_y = - self.move_yif self.collision_sign_x == 2 and self.move_x > 0:self.move_y = - self.move_yif self.distance < self.radius and self.collision_sign_y == 1 and self.collision_sign_x == 3:self.move_y = - self.move_y# 球在球拍左、右两侧中间的碰撞检测if self.distance < self.radius and self.collision_sign_y == 3:self.move_x = - self.move_x# 球与砖块的碰撞检测def ball_brick(self):# 定义碰撞标识self.collision_sign_bx = 0self.collision_sign_by = 0if self.ball_x < self.brick_x:self.closestpoint_bx = self.brick_xself.collision_sign_bx = 1elif self.ball_x > self.brick_x + self.brick_length:self.closestpoint_bx = self.brick_x + self.brick_lengthself.collision_sign_bx = 2else:self.closestpoint_bx = self.ball_xself.collision_sign_bx = 3if self.ball_y < self.brick_y:self.closestpoint_by = self.brick_yself.collision_sign_by = 1elif self.ball_y > self.brick_y + self.brick_wide:self.closestpoint_by = self.brick_y + self.brick_wideself.collision_sign_by = 2else:self.closestpoint_by = self.ball_yself.collision_sign_by = 3# 定义砖块到圆心最近点与圆心的距离self.distanceb = math.sqrt(math.pow(self.closestpoint_bx - self.ball_x, 2) + math.pow(self.closestpoint_by - self.ball_y, 2))# 球在砖块上左、上中、上右3种情况的碰撞检测if self.distanceb < self.radius and self.collision_sign_by == 1 and (self.collision_sign_bx == 1 or self.collision_sign_bx == 2):if self.collision_sign_bx == 1 and self.move_x > 0:self.move_x = - self.move_xself.move_y = - self.move_yif self.collision_sign_bx == 1 and self.move_x < 0:self.move_y = - self.move_yif self.collision_sign_bx == 2 and self.move_x < 0:self.move_x = - self.move_xself.move_y = - self.move_yif self.collision_sign_bx == 2 and self.move_x > 0:self.move_y = - self.move_yif self.distanceb < self.radius and self.collision_sign_by == 1 and self.collision_sign_bx == 3:self.move_y = - self.move_y# 球在砖块下左、下中、下右3种情况的碰撞检测if self.distanceb < self.radius and self.collision_sign_by == 2 and (self.collision_sign_bx == 1 or self.collision_sign_bx == 2):if self.collision_sign_bx == 1 and self.move_x > 0:self.move_x = - self.move_xself.move_y = - self.move_yif self.collision_sign_bx == 1 and self.move_x < 0:self.move_y = - self.move_yif self.collision_sign_bx == 2 and self.move_x < 0:self.move_x = - self.move_xself.move_y = - self.move_yif self.collision_sign_bx == 2 and self.move_x > 0:self.move_y = - self.move_yif self.distanceb < self.radius and self.collision_sign_by == 2 and self.collision_sign_bx == 3:self.move_y = - self.move_y# 球在砖块左、右两侧中间的碰撞检测if self.distanceb < self.radius and self.collision_sign_by == 3:self.move_x = - self.move_xclass Main(GameWindow, Rect, Ball, Brick, Collision, Score, Win, GameOver):'''创建主程序类'''def __init__(self, *args, **kw):super(Main, self).__init__(*args, **kw)super(GameWindow, self).__init__(*args, **kw)super(Rect, self).__init__(*args, **kw)super(Ball, self).__init__(*args, **kw)super(Brick, self).__init__(*args, **kw)super(Collision, self).__init__(*args, **kw)super(Score, self).__init__(*args, **kw)super(Win, self).__init__(*args, **kw)# 定义游戏开始标识start_sign = 0while True:self.backgroud()self.rectmove()self.countscore()if self.over_sign == 1 or self.win_sign == 1:break# 获取游戏窗口状态for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()if event.type == MOUSEBUTTONDOWN:pressed_array = pygame.mouse.get_pressed()if pressed_array[0]:start_sign = 1if start_sign == 0:self.ballready()else:self.ballmove()self.brickarrange()# 更新游戏窗口pygame.display.update()# 控制游戏窗口刷新频率time.sleep(0.010)if __name__ == '__main__':pygame.init()pygame.font.init()catchball = Main()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。