28行實(shí)現(xiàn)簡(jiǎn)易版反彈珠游戲

功能:

  1. 左右鍵控制反彈板移動(dòng);
  2. 越過底線就死亡;
  3. 空格鍵暫停,esc鍵退出。

代碼:

import pygame
import numpy as np
from sys import exit

screen = pygame.display.set_mode((600,600))
pygame.display.set_caption('巴拉巴拉小魔仙')
rabit = pygame.image.load('./Desktop/rabit_resize.jpg')
x,y,w,h = rabir_rect = rabit.get_rect()
x,y = 250,300
speed = [1,-1]
fps = 200
fclock = pygame.time.Clock()
board = board_x,board_y,board_w,board_h = pygame.draw.rect(screen,(255,0,0),[20, 550, 100, 20])

while True:
    fclock.tick(fps)
    board = board_x,board_y,board_w,board_h = pygame.draw.rect(screen,(255,0,0),[board_x, 550, 100, 20])
    for event in pygame.event.get():
        if event.type == pygame.QUIT:exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:exit()
            elif event.key == pygame.K_LEFT :board_x -= 20
            elif event.key == pygame.K_RIGHT :board_x += 20

    x += speed[0];y += speed[1]
    if x < 0 or x + w > 600 :speed[0] = -speed[0]
    if y < 0 or board_x < x < board_x +  board_w and y + h > board_y :speed[1] = -speed[1]

    screen.fill((255,255,255))
    board = pygame.draw.rect(screen,(255,0,0),[board_x, 550, 100, 20])
    screen.blit(rabit,(x,y))
    pygame.display.update()

可以改善的地方:

  1. 增加積分制,作為深度學(xué)習(xí)的激勵(lì)函數(shù);
  2. 可以增加小球數(shù)目,以增加難度。

樣本:

巴拉巴拉小魔仙反彈球
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容