树莓派基于PS2操纵杆的飞机大战小游戏

击中病毒LED灯闪烁
PS2操纵杆、PCF8591
PCF8591:
SDA - SDA
SCL - SCL
VCC - 5V
GND - GND
AIN1、AIN2、AIN3找空插即可

PS2操纵杆:
VRx - AIN1
VRy - AIN0
SW - AIN2
VCC - 3.3V
GND - GND

LED灯:
-一 接 17

  • S 接18
  • GND 接GND
    病毒自由掉落,子弹一直发射
    算是个初级版吧,还有很多功能可以完善
    代码如下:
import RPi.GPIO as GPIO
import PCF8591 as ADC
import pygame
import random
import sys
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17, GPIO.OUT)
pygame.init()
CREAT_VIRUS_EVENT = pygame.USEREVENT  # # 用户自定义事件
pygame.time.set_timer(CREAT_VIRUS_EVENT, 500)
Shoot = pygame.USEREVENT + 1
pygame.time.set_timer(Shoot, 200)
bg = pygame.image.load("/home/pi/CLBDEMO/飞机大战/bg.png")
bullet = pygame.image.load("/home/pi/CLBDEMO/飞机大战/bullet.png")
plane = pygame.image.load("/home/pi/CLBDEMO/飞机大战/plane.png")
virus = pygame.image.load("/home/pi/CLBDEMO/飞机大战/virus.png")
screen = pygame.display.set_mode((480, 550))
pygame.display.set_caption("打飞机")
x = 195
y = 476
posplan = (x, y)
screen.blit(bg, (0, 0))
screen.blit(plane, posplan)
pygame.display.update()
clock = pygame.time.Clock()
state = 'ready'
score = 0
# # 病毒的初始y坐标
vy = 0
lstb = []
lstv = []

def setup():
	ADC.setup(0x48)					# Setup PCF8591
setup()
fonta = pygame.font.SysFont('arial', 25)
sco = fonta.render('SCORE:', True, (255, 255, 255))
while True:
    GPIO.output(17, GPIO.HIGH)
    clock.tick(500)
    screen.blit(bg, (0, 0))
    screen.blit(plane, (x, y))
    screen.blit(sco, (150, 250))
    num = fonta.render(str(score), True, (255, 255, 255))
    screen.blit(num, (260, 250))
    if state == 'ready':
        score = 0
        flag = True
        x = 195
        y = 476
        screen.blit(bg, (0, 0))
        screen.blit(plane, (x, y))
        pygame.display.update()
    # # 子弹向前飞
    for i in lstb:
        i[1] -= 5
    for i in lstb:
        screen.blit(bullet, i)
        if i[1] <= 0: # # lim
            lstb.remove(i)
    # # 病毒往下坠,病毒出界则游戏状态变为ready
    for i in lstv:
        i[1] += 1
    for i in lstv:
        screen.blit(virus, i)
        if i[1] >= 476:
            print("病毒触底,英雄失败!再接再厉,再创辉煌!")
            print(score)
            flag = False
            state = 'ready'
            break
    for j in lstv:
        for i in lstb:
            if j[1] >= 0 and j[0] <= i[0] <= j[0] + virus.get_width() and j[1] < i[1] < j[1] + virus.get_height():
                lstb.remove(i)
                lstv.remove(j)
                GPIO.output(17, GPIO.LOW)
                time.sleep(0.05)
                score += 1
    # # 除了从天而降都可以碰撞
    for i in lstv:
        if i[0] <= x <= i[0]+virus.get_width() and i[1] <= y <= i[1]+virus.get_height():
            print("飞机碰到病毒,失败了!")
            print(score)
            flag = False
            state = 'ready'
            break
        if  i[0] <= x + plane.get_width() <= i[0]+virus.get_width() and i[1] <= y <= i[1]+virus.get_height():
            print("飞机碰到病毒,失败了!")
            print(score)
            flag = False
            state = 'ready'
            break
    for event in pygame.event.get():
        stepx = 0
        stepy = 0
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            state = 'running'
        if state == 'running':
            # # 随机生成病毒
            if event.type == CREAT_VIRUS_EVENT:
                lstv.append([random.randint(0, bg.get_width() - virus.get_width()), 0])
            # # 控制子弹飞行
            xb = x + plane.get_width()/2.6
            yb = y - bullet.get_height()
            if event.type == Shoot:
                lstb.append([xb, yb])
            # # 控制飞机移动
            if ADC.read(1) <= 5:
                if x > 0:
                    x -= 25
            if ADC.read(1) >= 250:
                if x + plane.get_width() < bg.get_width():
                    x += 25
            if ADC.read(0) <= 5:
                if y >= 0:
                    y -= 25
            if ADC.read(0) >= 250:
                if y + plane.get_height() < 550:
                    y += 25
    if flag == False:
        lstv.clear()  # # 出现问题以后把病毒列表清空
        lstb.clear()
    pygame.display.update()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值