功能:
- 左右鍵控制反彈板移動(dòng)蛆楞;
- 越過底線就死亡沥潭;
- 空格鍵暫停乏奥,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()
可以改善的地方:
- 增加積分制彤悔,作為深度學(xué)習(xí)的激勵(lì)函數(shù);
- 可以增加小球數(shù)目索守,以增加難度晕窑。
樣本:
巴拉巴拉小魔仙反彈球