功能:
- 左右键控制反弹板移动;
- 越过底线就死亡;
- 空格键暂停,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()
可以改善的地方:
- 增加积分制,作为深度学习的激励函数;
- 可以增加小球数目,以增加难度。
样本: