Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Brick Game with python pygame

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to play the brick game with python pygame". In the operation of the actual case, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The interface and code of the game environment are as follows

Import syssys.path.append (ringing E:\ anaconda\ Lib\ site-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 = 10scoresign0oversignsign0winsignsign0ballballcolour = (240240240) 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, (255255255), ((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) # drawing ball def ball_window (): global move_x global move_y # collision detection between the ball and the window frame 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-rect_wide): closestpoint_y = (window_wide-rect_wide) collision_sign_y = 1 elif ball_y >

Window_wide: closestpoint_y = window_wide collision_sign_y = 2 else: closestpoint_y = ball_y collision_sign_y = 3 # defines the distance between the racket and the center of the circle distance = math.sqrt (math.pow (closestpoint_x-ball_x,2) + math.pow (closestpoint_y-ball_y) 2) # collision detection if distance of the ball on the left, top, middle and right of the racket

< radius and collision_sign_y == 1 and (collision_sign_x == 1 or collision_sign_x == 2): if collision_sign_x == 1 and move_x >

0: move_x =-move_x move_y =-move_y if collision_sign_x = = 1 and move_x

< 0: move_y = - move_y if collision_sign_x == 2 and move_x < 0: move_x = - move_x move_y = - move_y if collision_sign_x == 2 and move_x >

0: move_y =-move_y if distance

< radius and collision_sign_y == 1 and collision_sign_x == 3: move_y = - move_y if distance < radius and collision_sign_y == 3: #球在球拍左、右两侧中间的碰撞检测 move_x = - move_x k_counter = k_counter + 1def ballmove(): global ball_x global ball_y global over_sign global frequency global brick_list #绘制球,设置反弹触发条件 pygame.draw.circle(game_window,ball_color,(ball_x,ball_y),radius) ball_x += move_x ball_y -= move_y #调用碰撞检测函数 ball_window() ball_rect() if distance < radius: frequency += 1 #接球次数 if ball_y >

Set the failure condition of the game over_sign = 1 def record_brick_state (): global brick_state global brick_list if ball_y = = 203: brick_state = list (_ flatten (brick_list)) # change to one-dimensional ball _ state = [0pl 0pl 0pl 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_ # draw bricks 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 (255255255), (brick_x,brick_y,brick_length,brick_wide)) ball_brick () # call the collision detection function if distanceb

< radius: brick_list[i][j] = 0 calculate_score(brick_list) #设置游戏胜利条件 if score == 60: win_sign = 1def ball_brick(): global distanceb global move_x global move_y #球与砖块的碰撞检测 collision_sign_bx = 0 #定义碰撞标识 collision_sign_by = 0 if ball_x < brick_x: closestpoint_bx = brick_x collision_sign_bx = 1 elif ball_x >

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: closestpoint_by = brick_y collision_sign_by = 1 elif 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 # defines the distance between the brick and the center of the circle distanceb = math.sqrt (math.pow (closestpoint_bx-ball_x,2) + math.pow (closestpoint_by-ball_y) 2)) # collision detection if distanceb of the ball on the left, upper middle and upper right of the brick

< radius and collision_sign_by == 1 and (collision_sign_bx == 1 or collision_sign_bx == 2): if collision_sign_bx == 1 and move_x >

0: move_x =-move_x move_y =-move_y if collision_sign_bx = = 1 and move_x

< 0: move_y = - move_y if collision_sign_bx == 2 and move_x < 0: move_x = - move_x move_y = - move_y if collision_sign_bx == 2 and move_x >

0: move_y =-move_y if distanceb

< radius and collision_sign_by == 1 and collision_sign_bx == 3: move_y = - move_y #球在砖块下左、下中、下右3种情况的碰撞检测 if distanceb < radius and collision_sign_by == 2 and (collision_sign_bx == 1 or collision_sign_bx == 2): if collision_sign_bx == 1 and move_x >

0: move_x =-move_x move_y =-move_y if collision_sign_bx = = 1 and move_x

< 0: move_y = - move_y if collision_sign_bx == 2 and move_x < 0: move_x = - move_x move_y = - move_y if collision_sign_bx == 2 and move_x >

0: move_y =-move_y if distanceb < radius and collision_sign_by = = 2 and collision_sign_bx = = 3: move_y =-move_y if distanceb < radius and collision_sign_by = = 3: # collision detection of the ball in the middle of the left and right sides of the brick move_x =-move_xwhile True: 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,1,1,1,1,1,1], [1,1,1,1,1,1,1,1,1,1,1,1]] score=0 win_sign=0 frequency=0 over_sign=0 mouse_x _ = pygame.mouse.get_pos () ball_x=mouse_x ball_y = window_wide-rect_wide-radius while True: game_window.fill (111111111) pygame.display.flip () for event in pygame.event.get (): if event.type = = pygame.QUIT: pygame.quit () exit () Rectmove () ballmove () brickarrange () record_brick_state () record_ball_state () if ball_state [1]! = 0: if ball_state [3]! = 0: if ball_state [5]! = 0: state = brick_state + ball_state ball_state = [0focus 0 Print (state) if over_sign = = 1: break if win_sign = = 1: break pygame.display.update () time.sleep (0.06) "how to play a brick game with python pygame" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report