今回は、Pygameでパックマン風のゲームを作りました。サークルでEne-1の計器を作っているときにPygameを使っていて、それの続きとして作ってみました。

素材
 使いたい人がもし仮にいたら勝手に使ってください。
・chip2
chip2
・coin
coin
・enemy-2
enemy-2
・message
message
・bon
bom
・step
step

・sozai-1
これだけは、自分では描けなかったので、(●MACK●http://homepage3.nifty.com/looseleaf/)様の素材を使っています。
sozai-1


プログラム
 今回はきれいなプログラムを書く気が起きなかったのと、書いてるうちにどこがどう動いているのかよくわからなくなってきたので、プログラムが雑です。
import pygame,sys
import random


pygame.init()
window_size = (640, 640)
bg_color = (0, 0, 255)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(window_size)
pygame.display.set_caption("GAME")

img_bg = pygame.image.load('chip2.png')
message = pygame.image.load('message.png')
charae = pygame.image.load('enemy-2.png')
chara = pygame.image.load('sozai-1.png')
bom = pygame.image.load('bom.png')
coin = pygame.image.load('coin.png')
step = pygame.image.load('step.png')
num_chips_per_line = int(img_bg.get_width() / 32)

map_data = [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,
1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,
1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,
1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,
1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1,
1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1,
1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1,
1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1,
1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1,
1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1,
1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1,
1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
]
i=0
chara_ex = [9,10,11,12]
chara_ey = [9,10,11,12]
chara_exa = [9,10,11,12]
chara_eya = [9,10,11,12]
coin_x = [1,1,1,1]
coin_y = [1,1,1,1]
coin_s = [0,0,0,0]
chara_x = 1
chara_y = 1
chara_xa = 1
chara_ya = 1
chara_xb = 1
chara_yb = 1
way_ex = [1,1,1,1]
way_ey = [1,1,1,1]
flame = 0
dir = "S"
dir_e = [3,3,3,3]
flag = 0
flag_1 = 0
flag_2 = 0
flag_3 = 0
walk = 0
deth = 0
c = 0
do = 0
bom_x = 0
bpm_y = 0
j = 0

#coin
for i in range(0,4):
while True:
coin_x[i] = random.randint(0, 19)
coin_y[i] = random.randint(0, 19)
if (map_data[coin_y[i] * 20 + coin_x[i]] == 0):
break

#step
while True:
step_x = random.randint(0, 19)
step_y = random.randint(0, 19)
if (map_data[step_y * 20 + step_x] == 0):
break

end_game = False
while not end_game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
end_game = True
#map
for y in range(0, 20):
for x in range(0, 20):
i = y * 20 + x
pos_x = x * 32
pos_y = y * 32
c = map_data[i]
dx = c % num_chips_per_line
dy = int(c / num_chips_per_line)
screen.blit(img_bg, (pos_x, pos_y), (32 * dx, 32* dy, 32,32))

#end event
for i in range(0,4):
if(chara_ex[i] == chara_x and chara_ey[i] == chara_y):
flag = 1

if(flag == 0 or flag == 2):

#my character
if (flame % 30 == 0):
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP]:
chara_y = max(chara_y - 1, 0)
if pressed[pygame.K_LEFT]:
chara_x = max(chara_x - 1, 0)
if pressed[pygame.K_DOWN]:
chara_y = min(chara_y + 1, 20 - 1)
if pressed[pygame.K_RIGHT]:
chara_x = min(chara_x + 1, 20 - 1)
walk = 1
if (chara_y == chara_yb and chara_x == chara_xb):
walk = 0

chara_xa = chara_x
chara_ya = chara_y

if (map_data[chara_y * 20 + chara_xb] == 1):
flag_1 = 1
if (map_data[chara_yb * 20 + chara_x] == 1):
flag_2 = 1
if (map_data[chara_y * 20 + chara_x] == 1):
flag_3 = 1

if (flag_3 == 1):
chara_x = chara_xb
chara_y = chara_yb
if (flag_1 == 0 and flag_2 == 1):
chara_y = chara_ya
elif (flag_1 == 1 and flag_2 == 0):
chara_x = chara_xa

if (chara_x > chara_xb):
dir = "E"
if (chara_x < chara_xb):
dir = "W"
if (chara_y > chara_yb):
dir = "S"
if (chara_y < chara_yb):
dir = "N"

#enemy
for i in range(0,4):

if (chara_exa[i] == chara_ex[i] and chara_eya[i] == chara_ey[i]):
while True:
chara_exa[i] = random.randint(0, 19)
chara_eya[i] = random.randint(0, 19)
if (map_data[chara_eya[i] * 20 + chara_exa[i]] == 0):
if (chara_ey[i] > chara_eya[i]):
way_ey[i] = 1
if (chara_ey[i] < chara_eya[i]):
way_ey[i] = 0
if (chara_ex[i] > chara_exa[i]):
way_ex[i] = 1
if (chara_ex[i] < chara_exa[i]):
way_ex[i] = 0
break
#event
if (dir_e[i] == 1):
if (chara_y - chara_ey[i] < 6 and chara_y > chara_ey[i]):
chara_exa[i] = chara_x
chara_eya[i] = chara_y
do = 1
if (chara_x - chara_ex[i] < 6 and chara_x > chara_ex[i]):
chara_exa[i] = chara_x
chara_eya[i] = chara_y
do = 1
if (chara_ex[i] - chara_x < 6 and chara_ex[i] > chara_x):
chara_exa[i] = chara_x
chara_eya[i] = chara_y
do = 1
if (dir_e[i] == 2):
if (chara_x - chara_ex[i] < 6 and chara_x > chara_ex[i]):
chara_exa[i] = chara_x
chara_eya[i] = chara_y
do = 1
if (chara_ey[i] - chara_y < 6 and chara_ey[i] > chara_y):
chara_exa[i] = chara_x
chara_eya[i] = chara_y
do = 1
if (chara_y - chara_ey[i] < 6 and chara_y > chara_ey[i]):
chara_exa[i] = chara_x
chara_eya[i] = chara_y
do = 1
if (dir_e[i] == 3):
if (chara_ey[i] - chara_y < 6 and chara_ey[i] > chara_y):
chara_exa[i] = chara_x
chara_eya[i] = chara_y
do = 1
if (chara_x - chara_ex[i] < 6 and chara_x > chara_ex[i]):
chara_exa[i] = chara_x
chara_eya[i] = chara_y
do = 1
if (chara_ex[i] - chara_x < 6 and chara_ex[i] > chara_x):
chara_exa[i] = chara_x
chara_eya[i] = chara_y
do = 1
if (dir_e[i] == 4):
if (chara_ex[i] - chara_x < 6 and chara_ex[i] > chara_x):
chara_exa[i] = chara_x
chara_eya[i] = chara_y
do = 1
if (chara_ey[i] - chara_y < 6 and chara_ey[i] > chara_y):
chara_exa[i] = chara_x
chara_eya[i] = chara_y
do = 1
if (chara_y - chara_ey[i] < 6 and chara_y > chara_ey[i]):
chara_exa[i] = chara_x
chara_eya[i] = chara_y
do = 1

if (do == 1):
if (chara_ey[i] > chara_eya[i]):
way_ey[i] = 1
if (chara_ey[i] < chara_eya[i]):
way_ey[i] = 0
if (chara_ex[i] > chara_exa[i]):
way_ex[i] = 1
if (chara_ex[i] < chara_exa[i]):
way_ex[i] = 0

if (flame % 30 == 0):
if (dir_e[i] % 2 == 1):
if (map_data[chara_ey[i] * 20 + chara_ex[i] + 1] == 0 and way_ex[i] == 0):
dir_e[i] = 2
if (chara_ey[i] >= chara_eya[i]):
way_ey[i] = 1
if (chara_ey[i] < chara_eya[i]):
way_ey[i] = 0
if (map_data[chara_ey[i] * 20 + chara_ex[i] - 1] == 0 and way_ex[i] == 1):
dir_e[i] = 4
if (chara_ey[i] >= chara_eya[i]):
way_ey[i] = 1
if (chara_ey[i] < chara_eya[i]):
way_ey[i] = 0
elif (dir_e[i] % 2 == 0):
if (map_data[chara_ey[i] * 20 + chara_ex[i] + 20] == 0 and way_ey[i] == 0):
dir_e[i] = 3
if (chara_ex[i] >= chara_exa[i]):
way_ex[i] = 1
if (chara_ex[i] < chara_exa[i]):
way_ex[i] = 0
if (map_data[chara_ey[i] * 20 + chara_ex[i] - 20] == 0 and way_ey[i] == 1):
dir_e[i] = 1
if (chara_ex[i] >= chara_exa[i]):
way_ex[i] = 1
if (chara_ex[i] < chara_exa[i]):
way_ex[i] = 0

if (flame % 30 == 0):
if (dir_e[i] == 1):
chara_ey[i] = chara_ey[i] - 1
elif (dir_e[i] == 2):
chara_ex[i] = chara_ex[i] + 1
elif (dir_e[i] == 3):
chara_ey[i] = chara_ey[i] + 1
elif (dir_e[i] == 4):
chara_ex[i] = chara_ex[i] - 1

flag_1 = 0
flag_2 = 0
flag_3 = 0
do = 0

# coin event
for i in range(0, 4):
if (coin_x[i] == chara_x and coin_y[i] == chara_y):
coin_s[i] = 1

if (coin_s[0] == 1 and coin_s[1] == 1 and coin_s[2] == 1 and coin_s[3] == 1):
flag = 2


#grafic
if(chara_x == step_x and chara_y == step_y and flag == 2):
flag = 4

for i in range(0,4):
if (coin_s[i] == 0):
screen.blit(coin, (coin_x[i] * 32,coin_y[i] * 32), (0, 0, 32, 32))

for i in range(0,4):
if (coin_s[i] == 0):
screen.blit(coin, (i * 32,0), (32, 0, 32, 32))
else:
screen.blit(coin, (i * 32,0), (0,0,32,32))

for i in range(0,4):
if (dir_e[i] == 1):
screen.blit(charae, (chara_ex[i] * 32, chara_ey[i] * 32), (64, 0, 32, 32))
elif (dir_e[i] == 2):
screen.blit(charae, (chara_ex[i] * 32, chara_ey[i] * 32), (32, 0, 32, 32))
elif (dir_e[i] == 3):
screen.blit(charae, (chara_ex[i] * 32, chara_ey[i] * 32), (96, 0, 32, 32))
elif (dir_e[i] == 4):
screen.blit(charae, (chara_ex[i] * 32, chara_ey[i] * 32), (0, 0, 32, 32))

if (flag == 2):
screen.blit(step, (step_x * 32, step_y * 32), (0, 0, 32, 32))

if (dir == "S"):
if (walk == 0):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (32, 0, 32, 64))
elif (flame % 120 > 89):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (32, 0, 32, 64))
elif (flame % 120 > 59):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (0, 0, 32, 64))
elif (flame % 120 > 29):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (32, 0, 32, 64))
elif (flame % 120 < 30):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (64, 0, 32, 64))
elif (dir == "E"):
if (walk == 0):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (32, 128, 32, 64))
elif (flame % 120 > 89):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (32, 128, 32, 64))
elif (flame % 120 > 59):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (0, 128, 32, 64))
elif (flame % 120 > 29):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (32, 128, 32, 64))
elif (flame % 120 < 30):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (64, 128, 32, 64))
elif (dir == "N"):
if (walk == 0):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (32, 192, 32, 64))
elif (flame % 120 > 89):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (32, 192, 32, 64))
elif (flame % 120 > 59):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (0, 192, 32, 64))
elif (flame % 120 > 29):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (32, 192, 32, 64))
elif (flame % 120 < 30):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (64, 192, 32, 64))
elif (dir == "W"):
if (walk == 0):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (32, 64, 32, 64))
elif (flame % 120 > 89):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (32, 64, 32, 64))
elif (flame % 120 > 59):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (0, 64, 32, 64))
elif (flame % 120 > 29):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (32, 64, 32, 64))
elif (flame % 120 < 30):
screen.blit(chara, (chara_x * 32, chara_y * 32 - 32), (64, 64, 32, 64))

elif(flag == 1):
if(j<10):
screen.blit(bom, (chara_x * 32, chara_y * 32), (0, 0, 32, 32))
elif(j<20):
screen.blit(bom, (chara_x * 32, chara_y * 32), (32 * 1, 0, 32, 32))
elif(j<30):
screen.blit(bom, (chara_x * 32, chara_y * 32), (32 * 2, 0, 32, 32))
elif(j<40):
screen.blit(bom, (chara_x * 32, chara_y * 32), (32 * 3, 0, 32, 32))
elif(j<50):
screen.blit(bom, (chara_x * 32, chara_y * 32), (32 * 4, 0, 32, 32))
elif(j<60):
screen.blit(bom, (chara_x * 32, chara_y * 32), (32 * 5, 0, 32, 32))
elif(j>119):
end_game = True
j = j + 1

if(flag == 4):
if(j < 120):
screen.blit(message, (120,240), (0, 0, 400, 64))
else:
end_game = True
j = j + 1

#for i in range(0,4):
# screen.blit(charae, (chara_exa[i] * 32,chara_eya[i] * 32), (128,0,32,32))
pygame.display.flip()
clock.tick(60)
flame = flame + 1
chara_xb = chara_x
chara_yb = chara_y

 かなり適当に作ったので、雑なところが多々ありますが、きちんと動き、それなりに面白かったので満足しています。

 なんかすごくlivedoorの編集ページが重くなってきたのでこれくらいにしておきます。
 それではまた今度。