python pygame制作打砖块游戏的具体代码是什么
Admin 2022-09-09 群英技术资讯 244 次浏览
本文实例为大家分享了python pygame实现打砖块游戏的具体代码,供大家参考,具体内容如下
最近在尝试着写一个用强化学习的方法玩打砖块的游戏,首先将游戏环境做些改动,以便产生需要的数据
游戏环境的界面以及代码如下
import sys sys.path.append(r'E:\anaconda\Lib\site-packages') import pygame import sys import random import time import math from tkinter import _flatten pygame.init() pygame.font.init() brick_length = 25 brick_wide = 15 rect_length = 100 rect_wide = 5 window_length = 400 window_wide = 250 move_x = 8 move_y = 8 radius = 10 score=0 over_sign=0 win_sign=0 frequency=0 ball_color=(240,240,240) k_counter = 0 state = [] 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 <= radius or ball_x >= (window_length-radius): move_x = -move_x if ball_y <= radius: move_y = -move_y def ball_rect(): #球与球拍的碰撞检测 collision_sign_x = 0 #定义碰撞标识 collision_sign_y = 0 global k_counter global move_x global move_y global distance mouse_x , _ = pygame.mouse.get_pos() if ball_x < (mouse_x-rect_length//2): closestpoint_x = mouse_x-rect_length//2 collision_sign_x = 1 elif ball_x > (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 #定义球拍到圆心最近点与圆心的距离 distance = math.sqrt(math.pow(closestpoint_x-ball_x,2)+math.pow(closestpoint_y-ball_y,2)) #球在球拍上左、上中、上右3种情况的碰撞检测 if distance < 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 + 1 def 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 > 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 < radius: brick_list[i][j] = 0 calculate_score(brick_list) #设置游戏胜利条件 if score == 60: win_sign = 1 def 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 #定义砖块到圆心最近点与圆心的距离 distanceb = math.sqrt(math.pow(closestpoint_bx-ball_x,2)+math.pow(closestpoint_by-ball_y,2)) #球在砖块上左、上中、上右3种情况的碰撞检测 if distanceb < 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: #球在砖块左、右两侧中间的碰撞检测 move_x = - move_x while 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], [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((111,111,111)) 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 =[0,0,0,0,0,0] print(state) if over_sign == 1: break if win_sign == 1: break pygame.display.update() time.sleep(0.06) 现原理
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
Python常用的魔法方法有几种?用法是什么?新手学习Python,可能对于算数运算符的魔法方法和反运算相关的魔法方法不是很了解,对此,下面小编就给大家介绍一下Python常用的魔法方法,感兴趣的朋友就继续往下看吧。
这篇文章主要为大家介绍了python日期时间处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
Python内置函数-range() 函数。Python3 range() 函数返回的是一个可迭代对象(类型是对象),而不是列表类型, 所以打印的时候不会打印列表。
这篇文章主要介绍了如何利用python实现Simhash算法,文章基于python的相关资料展开Simhash算法的详细介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下
这篇文章主要介绍了python中常见进制之间的转换方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008