pygameでブロック崩し
- 2014.02.08
- Raspberry Pi

前回の記事で、Arduinoを経由したセンサーデータの取得方法を紹介しましたが、そのデータを使って何かしたいと考えて、ゲームの入力インターフェイスとして使えるのではないかと思い、とりあえずゲームを作ってみました。
作ったゲームはブロック崩しで、pygameというpython用のライブラリを使いました。
以下、ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
import pygame from pygame.locals import * import sys width=640 height=480 SCREEN_SIZE=(width,height) class ball: def __init__(self,x,y,vx,vy,r): self.x=x self.y=y self.vx=vx self.vy=vy self.r=r def update(self): self.x+=self.vx self.y+=self.vy if self.x-self.r<0 or self.x+self.r>width: self.vx*=-1 if self.y-self.r<0 or self.y+self.r>height: self.vy*=-1 def draw(self,screen): pygame.draw.circle(screen,(255,0,0),(self.x,self.y),self.r) class brick: def __init__(self,x,y,w,h): self.x=x self.y=y self.w=w self.h=h self.flag=True def update(self,bx,by,r): if self.flag: if self.x<=bx+r and self.x+self.w>=bx-r: if self.y<=by+r and self.y+self.h>=by-r: self.flag=False ball_.vy*=-1 def draw(self,screen): if self.flag: pygame.draw.rect(screen,(255,255,255),Rect(self.x,self.y,self.w,self.h)) class bar: def __init__(self,x,y,w,h): self.x=x self.y=y self.w=w self.h=h def move(self,dx): self.x+=dx def update(self,bx,by,r): if self.x<=bx+r and self.x+self.w>=bx-r: if self.y<=by+r and self.y+self.h>=by-r: ball_.vy*=-1 def draw(self,screen): pygame.draw.rect(screen,(0,0,255),Rect(self.x,self.y,self.w,self.h)) ball_=ball(320,240,3,3,7) def main(): pygame.init() screen=pygame.display.set_mode(SCREEN_SIZE) pygame.display.set_caption("Breakout") clock=pygame.time.Clock() bar_=bar( (width-100)/2,height-50,100,10) bricks=[] for i in range(5): for j in range(10): bricks.append(brick(10*(j+1)+53*j,20*(i+1)+10*i,53,10)) while True: clock.tick(60) pressed_keys=pygame.key.get_pressed() if pressed_keys[K_LEFT]: bar_.move(-5) if pressed_keys[K_RIGHT]: bar_.move(5) ball_.update() bar_.update(ball_.x,ball_.y,ball_.r) for i in range(50): bricks[i].update(ball_.x,ball_.y,ball_.r) screen.fill( (0,0,0) ) ball_.draw(screen) bar_.draw(screen) for i in range(len(bricks)): bricks[i].draw(screen) pygame.display.update() for event in pygame.event.get(): if event.type==QUIT: sys.exit() if event.type==KEYDOWN: if event.key==K_ESCAPE: sys.exit() if __name__=="__main__": main() |
普段、pythonを使わないので、入門書を読みながら書きました。
ゲームーバーも、リトライ機能もなく、ゲームとはいえないようなものですが、とりあえず、ブロック崩しっぽくなっています。
上記コードの場合、バーの操作はキーボードですが、時間が出来た時にでも、何らかのセンサー値を使うように改良したいと思います。
-
前の記事
Raspberry PiでMinecraft起動 2014.02.08
-
次の記事
Raspberry PiにSSH接続 2014.02.08