PHP前端开发

python pygame怎么实现打砖块游戏

百变鹏仔 2个月前 (01-21) #Python
文章标签 打砖块

游戏环境的界面以及代码如下

import syssys.path.append(r'E:nacondaLibsite-packages')import pygameimport sysimport randomimport timeimport mathfrom tkinter import _flattenpygame.init()pygame.font.init()brick_length = 25brick_wide = 15rect_length = 100rect_wide = 5window_length = 400                                                           window_wide = 250move_x = 8                                                                   move_y = 8radius = 10score=0over_sign=0win_sign=0frequency=0ball_color=(240,240,240)k_counter = 0state = []game_window = pygame.display.set_mode((window_length,window_wide))def rectmove():    mouse_x , _ = pygame.mouse.get_pos()    pygame.draw.rect(game_window,(255,255,255),((mouse_x-rect_length//2),(window_wide-rect_wide),rect_length,rect_wide)) def ballready():                                                                            pygame.draw.circle(game_window,ball_color,(ball_x,ball_y),radius)            #绘制球def ball_window():     global move_x    global move_y   #球与窗口边框的碰撞检测    if ball_x = (window_length-radius):                move_x = -move_x    if ball_y  (mouse_x+rect_length//2):        closestpoint_x = mouse_x+rect_length//2        collision_sign_x = 2    else:        closestpoint_x = ball_x        collision_sign_x = 3    if ball_y  window_wide:        closestpoint_y = window_wide        collision_sign_y = 2    else:        closestpoint_y = ball_y        collision_sign_y = 3        #定义球拍到圆心最近点与圆心的距离    distance = math.sqrt(math.pow(closestpoint_x-ball_x,2)+math.pow(closestpoint_y-ball_y,2))        #球在球拍上左、上中、上右3种情况的碰撞检测    if distance  0:            move_x = - move_x            move_y = - move_y        if collision_sign_x == 1 and move_x  0:            move_y = - move_y    if distance  270:       #设置游戏失败条件        over_sign = 1      def record_brick_state():    global brick_state    global brick_list    if ball_y == 203:        brick_state = list(_flatten(brick_list))    #变为一维ball_state = [0,0,0,0,0,0]def record_ball_state():    global ball_x    global ball_y     global ball_state    if ball_y == 203:        ball_state[0] = ball_x*0.01        ball_state[1] = ball_y*0.01    if ball_y == 211:        ball_state[2] = ball_x*0.01        ball_state[3] = ball_y*0.01          if ball_y == 219:        ball_state[4] = ball_x*0.01        ball_state[5] = ball_y*0.01  def calculate_score(brick_list):    brick_num = 0    global score    for i in range(5):        for j in range(12):            brick_num = brick_num + brick_list[i][j]    score = 60 - brick_num#    print(score)    def brickarrange():    global brick_length    global brick_wide    global score        global win_sign    global brick_x    global brick_y    global distanceb    global ball_x    global ball_y      global brick_list_      #绘制砖块    for i in range(5):        for j in range(12):            brick_x = j*(brick_length+5)            brick_y = i*(brick_wide+5)+40            if brick_list[i][j] == 1:                                                                                              pygame.draw.rect(game_window,(255,255,255),(brick_x,brick_y,brick_length,brick_wide))                                                        ball_brick()      #调用碰撞检测函数                                        if distanceb  brick_x+brick_length:        closestpoint_bx = brick_x+brick_length        collision_sign_bx = 2    else:        closestpoint_bx = ball_x        collision_sign_bx = 3    if ball_y  brick_y+brick_wide:        closestpoint_by = brick_y+brick_wide        collision_sign_by = 2    else:        closestpoint_by = ball_y        collision_sign_by = 3                                                   #定义砖块到圆心最近点与圆心的距离    distanceb = math.sqrt(math.pow(closestpoint_bx-ball_x,2)+math.pow(closestpoint_by-ball_y,2))                                                                                #球在砖块上左、上中、上右3种情况的碰撞检测    if distanceb  0:            move_x = - move_x            move_y = - move_y        if collision_sign_bx == 1 and move_x  0:            move_y = - move_y    if distanceb  0:            move_x = - move_x            move_y = - move_y        if collision_sign_bx == 1 and move_x  0:            move_y = - move_y    if distanceb