鱼C论坛

 找回密码
 立即注册
查看: 1458|回复: 2

[作品展示] 升级了2

[复制链接]
发表于 2019-8-19 18:27:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #导入模块
  2. import pygame
  3. from pygame.locals import *
  4. import sys,random,time,math

  5. class GameWindow(object):
  6.         '''创建游戏窗口类'''
  7.         def __init__(self,*args,**kw):               
  8.                 self.window_length = 600
  9.                 self.window_wide = 500
  10.                 #绘制游戏窗口,设置窗口尺寸
  11.                 self.game_window = pygame.display.set_mode((self.window_length,self.window_wide))
  12.                 #设置游戏窗口标题
  13.                 pygame.display.set_caption("CatchBallGame")
  14.                 #定义游戏窗口背景颜色参数
  15.                 self.window_color = (135,206,250)

  16.         def backgroud(self):
  17.                 #绘制游戏窗口背景颜色
  18.                 self.game_window.fill(self.window_color)

  19. class Ball(object):
  20.         '''创建球类'''
  21.         def __init__(self,*args,**kw):
  22.                 #设置球的半径、颜色、移动速度参数
  23.                 self.ball_color = (255,215,0)               
  24.                 self.move_x = 1
  25.                 self.move_y = 1
  26.                 self.radius = 10

  27.         def ballready(self):
  28.                 #设置球的初始位置、
  29.                 self.ball_x = self.mouse_x
  30.                 self.ball_y = self.window_wide-self.rect_wide-self.radius
  31.                 #绘制球,设置反弹触发条件                       
  32.                 pygame.draw.circle(self.game_window,self.ball_color,(self.ball_x,self.ball_y),self.radius)

  33.         def ballmove(self):
  34.                 #绘制球,设置反弹触发条件                       
  35.                 pygame.draw.circle(self.game_window,self.ball_color,(self.ball_x,self.ball_y),self.radius)               
  36.                 self.ball_x += self.move_x
  37.                 self.ball_y -= self.move_y
  38.                 #调用碰撞检测函数
  39.                 self.ball_window()
  40.                 self.ball_rect()
  41.                 #每接5次球球速增加一倍
  42.                 if self.distance < self.radius:
  43.                         self.frequency += 1
  44.                         if self.frequency == 5:
  45.                                 self.frequency = 0
  46.                                 self.move_x += self.move_x
  47.                                 self.move_y += self.move_y
  48.                                 self.point += self.point
  49.                 #设置游戏失败条件
  50.                 if self.ball_y > 520:
  51.                         self.gameover = self.over_font.render("Game Over",False,(0,0,0))
  52.                         self.game_window.blit(self.gameover,(100,130))
  53.                         self.over_sign = 1

  54. class Rect(object):
  55.         '''创建球拍类'''
  56.         def __init__(self,*args,**kw):
  57.                 #设置球拍颜色参数
  58.                 self.rect_color = (255,0,0)
  59.                 self.rect_length = 100
  60.                 self.rect_wide = 10

  61.         def rectmove(self):
  62.                 #获取鼠标位置参数
  63.                 self.mouse_x,self.mouse_y = pygame.mouse.get_pos()
  64.                 #绘制球拍,限定横向边界                                       
  65.                 if self.mouse_x >= self.window_length-self.rect_length//2:
  66.                         self.mouse_x = self.window_length-self.rect_length//2
  67.                 if self.mouse_x <= self.rect_length//2:
  68.                         self.mouse_x = self.rect_length//2
  69.                 pygame.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))

  70. class Brick(object):
  71.         def __init__(self,*args,**kw):
  72.                 #设置砖块颜色参数
  73.                 self.brick_color = (139,126,102)
  74.                 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]]
  75.                 self.brick_length = 80
  76.                 self.brick_wide = 20

  77.         def brickarrange(self):               
  78.                 for i in range(5):
  79.                         for j in range(6):
  80.                                 self.brick_x = j*(self.brick_length+24)
  81.                                 self.brick_y = i*(self.brick_wide+20)+40
  82.                                 if self.brick_list[i][j] == 1:
  83.                                         #绘制砖块
  84.                                         pygame.draw.rect(self.game_window,self.brick_color,(self.brick_x,self.brick_y,self.brick_length,self.brick_wide))                                       
  85.                                         #调用碰撞检测函数
  86.                                         self.ball_brick()                                                                               
  87.                                         if self.distanceb < self.radius:
  88.                                                 self.brick_list[i][j] = 0
  89.                                                 self.score += self.point
  90.                 #设置游戏胜利条件
  91.                 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]]:
  92.                         self.win = self.win_font.render("You Win",False,(0,0,0))
  93.                         self.game_window.blit(self.win,(100,130))
  94.                         self.win_sign = 1

  95. class Score(object):
  96.         '''创建分数类'''
  97.         def __init__(self,*args,**kw):               
  98.                 #设置初始分数
  99.                 self.score = 0
  100.                 #设置分数字体
  101.                 self.score_font = pygame.font.SysFont('arial',20)
  102.                 #设置初始加分点数
  103.                 self.point = 1
  104.                 #设置初始接球次数
  105.                 self.frequency = 0

  106.         def countscore(self):
  107.                 #绘制玩家分数                       
  108.                 my_score = self.score_font.render(str(self.score),False,(255,255,255))
  109.                 self.game_window.blit(my_score,(555,15))

  110. class GameOver(object):
  111.         '''创建游戏结束类'''
  112.         def __init__(self,*args,**kw):
  113.                 #设置Game Over字体
  114.                 self.over_font = pygame.font.SysFont('arial',80)
  115.                 #定义GameOver标识
  116.                 self.over_sign = 0

  117. class Win(object):
  118.         '''创建游戏胜利类'''
  119.         def __init__(self,*args,**kw):
  120.                 #设置You Win字体
  121.                 self.win_font = pygame.font.SysFont('arial',80)
  122.                 #定义Win标识
  123.                 self.win_sign = 0

  124. class Collision(object):
  125.         '''碰撞检测类'''
  126.         #球与窗口边框的碰撞检测
  127.         def ball_window(self):
  128.                 if self.ball_x <= self.radius or self.ball_x >= (self.window_length-self.radius):
  129.                         self.move_x = -self.move_x
  130.                 if self.ball_y <= self.radius:
  131.                         self.move_y = -self.move_y

  132.         #球与球拍的碰撞检测
  133.         def ball_rect(self):
  134.                 #定义碰撞标识
  135.                 self.collision_sign_x = 0
  136.                 self.collision_sign_y = 0

  137.                 if self.ball_x < (self.mouse_x-self.rect_length//2):
  138.                         self.closestpoint_x = self.mouse_x-self.rect_length//2
  139.                         self.collision_sign_x = 1
  140.                 elif self.ball_x > (self.mouse_x+self.rect_length//2):
  141.                         self.closestpoint_x = self.mouse_x+self.rect_length//2
  142.                         self.collision_sign_x = 2
  143.                 else:
  144.                         self.closestpoint_x = self.ball_x
  145.                         self.collision_sign_x = 3

  146.                 if self.ball_y < (self.window_wide-self.rect_wide):
  147.                         self.closestpoint_y = (self.window_wide-self.rect_wide)
  148.                         self.collision_sign_y = 1
  149.                 elif self.ball_y > self.window_wide:
  150.                         self.closestpoint_y = self.window_wide
  151.                         self.collision_sign_y = 2
  152.                 else:
  153.                         self.closestpoint_y = self.ball_y
  154.                         self.collision_sign_y = 3
  155.                 #定义球拍到圆心最近点与圆心的距离
  156.                 self.distance = math.sqrt(math.pow(self.closestpoint_x-self.ball_x,2)+math.pow(self.closestpoint_y-self.ball_y,2))
  157.                 #球在球拍上左、上中、上右3种情况的碰撞检测
  158.                 if self.distance < self.radius and self.collision_sign_y == 1 and (self.collision_sign_x == 1 or self.collision_sign_x == 2):
  159.                         if self.collision_sign_x == 1 and self.move_x > 0:
  160.                                 self.move_x = - self.move_x
  161.                                 self.move_y = - self.move_y
  162.                         if self.collision_sign_x == 1 and self.move_x < 0:
  163.                                 self.move_y = - self.move_y
  164.                         if self.collision_sign_x == 2 and self.move_x < 0:
  165.                                 self.move_x = - self.move_x
  166.                                 self.move_y = - self.move_y
  167.                         if self.collision_sign_x == 2 and self.move_x > 0:
  168.                                 self.move_y = - self.move_y
  169.                 if self.distance < self.radius and self.collision_sign_y == 1 and self.collision_sign_x == 3:
  170.                         self.move_y = - self.move_y
  171.                 #球在球拍左、右两侧中间的碰撞检测
  172.                 if self.distance < self.radius and self.collision_sign_y == 3:
  173.                         self.move_x = - self.move_x

  174.         #球与砖块的碰撞检测
  175.         def ball_brick(self):
  176.                 #定义碰撞标识
  177.                 self.collision_sign_bx = 0
  178.                 self.collision_sign_by = 0

  179.                 if self.ball_x < self.brick_x:
  180.                         self.closestpoint_bx = self.brick_x
  181.                         self.collision_sign_bx = 1
  182.                 elif self.ball_x > self.brick_x+self.brick_length:
  183.                         self.closestpoint_bx = self.brick_x+self.brick_length
  184.                         self.collision_sign_bx = 2
  185.                 else:
  186.                         self.closestpoint_bx = self.ball_x
  187.                         self.collision_sign_bx = 3

  188.                 if self.ball_y < self.brick_y:
  189.                         self.closestpoint_by = self.brick_y
  190.                         self.collision_sign_by = 1
  191.                 elif self.ball_y > self.brick_y+self.brick_wide:
  192.                         self.closestpoint_by = self.brick_y+self.brick_wide
  193.                         self.collision_sign_by = 2
  194.                 else:
  195.                         self.closestpoint_by = self.ball_y
  196.                         self.collision_sign_by = 3
  197.                 #定义砖块到圆心最近点与圆心的距离
  198.                 self.distanceb = math.sqrt(math.pow(self.closestpoint_bx-self.ball_x,2)+math.pow(self.closestpoint_by-self.ball_y,2))
  199.                 #球在砖块上左、上中、上右3种情况的碰撞检测
  200.                 if self.distanceb < self.radius and self.collision_sign_by == 1 and (self.collision_sign_bx == 1 or self.collision_sign_bx == 2):
  201.                         if self.collision_sign_bx == 1 and self.move_x > 0:
  202.                                 self.move_x = - self.move_x
  203.                                 self.move_y = - self.move_y
  204.                         if self.collision_sign_bx == 1 and self.move_x < 0:
  205.                                 self.move_y = - self.move_y
  206.                         if self.collision_sign_bx == 2 and self.move_x < 0:
  207.                                 self.move_x = - self.move_x
  208.                                 self.move_y = - self.move_y
  209.                         if self.collision_sign_bx == 2 and self.move_x > 0:
  210.                                 self.move_y = - self.move_y
  211.                 if self.distanceb < self.radius and self.collision_sign_by == 1 and self.collision_sign_bx == 3:
  212.                         self.move_y = - self.move_y
  213.                 #球在砖块下左、下中、下右3种情况的碰撞检测
  214.                 if self.distanceb < self.radius and self.collision_sign_by == 2 and (self.collision_sign_bx == 1 or self.collision_sign_bx == 2):
  215.                         if self.collision_sign_bx == 1 and self.move_x > 0:
  216.                                 self.move_x = - self.move_x
  217.                                 self.move_y = - self.move_y
  218.                         if self.collision_sign_bx == 1 and self.move_x < 0:
  219.                                 self.move_y = - self.move_y
  220.                         if self.collision_sign_bx == 2 and self.move_x < 0:
  221.                                 self.move_x = - self.move_x
  222.                                 self.move_y = - self.move_y
  223.                         if self.collision_sign_bx == 2 and self.move_x > 0:
  224.                                 self.move_y = - self.move_y
  225.                 if self.distanceb < self.radius and self.collision_sign_by == 2 and self.collision_sign_bx == 3:
  226.                         self.move_y = - self.move_y
  227.                 #球在砖块左、右两侧中间的碰撞检测
  228.                 if self.distanceb < self.radius and self.collision_sign_by == 3:
  229.                         self.move_x = - self.move_x

  230. class Main(GameWindow,Rect,Ball,Brick,Collision,Score,Win,GameOver):
  231.         '''创建主程序类'''
  232.         def __init__(self,*args,**kw):               
  233.                 super(Main,self).__init__(*args,**kw)
  234.                 super(GameWindow,self).__init__(*args,**kw)
  235.                 super(Rect,self).__init__(*args,**kw)
  236.                 super(Ball,self).__init__(*args,**kw)
  237.                 super(Brick,self).__init__(*args,**kw)
  238.                 super(Collision,self).__init__(*args,**kw)               
  239.                 super(Score,self).__init__(*args,**kw)
  240.                 super(Win,self).__init__(*args,**kw)
  241.                 #定义游戏开始标识
  242.                 start_sign = 0

  243.                 while True:                       
  244.                         self.backgroud()
  245.                         self.rectmove()
  246.                         self.countscore()                       
  247.                        
  248.                         if self.over_sign == 1 or self.win_sign == 1:
  249.                                 break
  250.                         #获取游戏窗口状态
  251.                         for event in pygame.event.get():
  252.                                 if event.type == pygame.QUIT:
  253.                                         sys.exit()
  254.                                 if event.type == MOUSEBUTTONDOWN:
  255.                                         pressed_array = pygame.mouse.get_pressed()
  256.                                         if pressed_array[0]:
  257.                                                 start_sign = 1
  258.                         if start_sign == 0:
  259.                                 self.ballready()
  260.                         else:
  261.                                 self.ballmove()

  262.                         self.brickarrange()

  263.                         #更新游戏窗口
  264.                         pygame.display.update()
  265.                         #控制游戏窗口刷新频率
  266.                         time.sleep(0.010)

  267. if __name__ == '__main__':
  268.         pygame.init()
  269.         pygame.font.init()
  270.         catchball = Main()
复制代码


游客,如果您要查看本帖隐藏内容请回复

评分

参与人数 1荣誉 +2 贡献 +1 收起 理由
zltzlt + 2 + 1 无条件支持楼主!

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-8-19 18:29:48 | 显示全部楼层
试着玩玩吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-19 18:31:13 | 显示全部楼层
谢谢

好玩不
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-4 15:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表