欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python?pygame實現(xiàn)打磚塊游戲

 更新時間:2022年05月11日 17:22:31   作者:小帆97  
這篇文章主要為大家詳細介紹了python?pygame實現(xiàn)打磚塊游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python pygame實現(xiàn)打磚塊游戲的具體代碼,供大家參考,具體內容如下

最近在嘗試著寫一個用強化學習的方法玩打磚塊的游戲,首先將游戲環(huán)境做些改動,以便產(chǎn)生需要的數(shù)據(jù)

游戲環(huán)境的界面以及代碼如下

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? ? ? ? ? #繪制球,設置反彈觸發(fā)條件?? ?
? ? pygame.draw.circle(game_window,ball_color,(ball_x,ball_y),radius)?? ??? ?
? ? ball_x += move_x
? ? ball_y -= move_y? ?#調用碰撞檢測函數(shù)
? ? ball_window()
? ? ball_rect()
? ? if distance < radius:
? ? ? ? frequency += 1? ? ? ? ? ?#接球次數(shù)?? ?
? ? 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)) ? ?#變?yōu)橐痪S

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()? ? ? #調用碰撞檢測函數(shù)?? ??? ??? ??? ??? ??? ?
? ? ? ? ? ? ? ? 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) ?

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Python反射用法實例簡析

    Python反射用法實例簡析

    這篇文章主要介紹了Python反射用法,結合實例形式簡單分析了Python反射的概念、原理及使用方法,需要的朋友可以參考下
    2017-12-12
  • Python黑魔法庫安裝及操作字典示例詳解

    Python黑魔法庫安裝及操作字典示例詳解

    這篇文章主要為大家介紹了Python中黑魔法庫的安裝及操作字典的示例詳解,有需要的 朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2021-10-10
  • Django Serializer HiddenField隱藏字段實例

    Django Serializer HiddenField隱藏字段實例

    這篇文章主要介紹了Django Serializer HiddenField隱藏字段實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • python運算符號詳細介紹

    python運算符號詳細介紹

    大家好,本篇文章主要講的是python運算符號詳細介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Python3讀取zip文件信息的方法

    Python3讀取zip文件信息的方法

    這篇文章主要介紹了Python3讀取zip文件信息的方法,涉及Python3 使用zipfile模塊操作zip文件的相關技巧,需要的朋友可以參考下
    2015-05-05
  • 如何將python的數(shù)據(jù)存儲到mysql數(shù)據(jù)庫中

    如何將python的數(shù)據(jù)存儲到mysql數(shù)據(jù)庫中

    在很多數(shù)據(jù)處理項目中,將數(shù)據(jù)存儲到數(shù)據(jù)庫中是非常常見的操作,下面這篇文章主要給大家介紹了關于如何將python的數(shù)據(jù)存儲到mysql數(shù)據(jù)庫中的相關資料,需要的朋友可以參考下
    2023-12-12
  • 解決python3 HTMLTestRunner測試報告中文亂碼的問題

    解決python3 HTMLTestRunner測試報告中文亂碼的問題

    今天小編就為大家分享一篇解決python3 HTMLTestRunner測試報告中文亂碼的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • python統(tǒng)計函數(shù)被調用次數(shù)的實現(xiàn)

    python統(tǒng)計函數(shù)被調用次數(shù)的實現(xiàn)

    本文主要介紹了python如何統(tǒng)計函數(shù)被調用次數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • 優(yōu)化Python代碼使其加快作用域內的查找

    優(yōu)化Python代碼使其加快作用域內的查找

    這篇文章主要介紹了優(yōu)化Python代碼使其加快作用域內的搜索,文中介紹了CPython相關的C代碼來對查找功能進行優(yōu)化,加快搜索的速度,需要的朋友可以參考下
    2015-03-03
  • Django實現(xiàn)簡單登錄的示例代碼

    Django實現(xiàn)簡單登錄的示例代碼

    本文主要介紹了Django實現(xiàn)簡單登錄的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11

最新評論