28行实现简易版反弹珠游戏

功能:

  1. 左右键控制反弹板移动;
  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. 增加积分制,作为深度学习的激励函数;
  2. 可以增加小球数目,以增加难度。

样本:

巴拉巴拉小魔仙反弹球
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容