Pythonでシューティングゲームを作っています。
とりあえず自機を描画し、方向キー入力で移動、Zキーでショットを撃つところまでは作れたのですが、その動作を同時に行うことができません。(具体的に言うと、Zキーを押しっぱなしにしてショットを撃っている最中に方向キーを押すと、自機が移動するがショットは中断されていまいます)
ショットを撃ちながら移動できるようにしたいのですが、どうすればいいのでしょうか?
ソースコードはこちらです
Python3.7
1import sys 2import pygame 3from pygame.locals import * 4 5HORIZON = 800 6VERTICAL = 500 7P_MAX_SHOT = 11 8P_MAX_BULLET = 15 9ShotLevel=11 10positionx = [0,-10,10,-10,10,-15,15,-10,10,-10,10] 11positiony = [-15,-2,-2,6,6,-6,-6,2,2,10,10] 12P_sub = [0,-1,1,-3,3,0,0,-2,2,-5,5] 13 14pygame.init() 15screen = pygame.display.set_mode((HORIZON, VERTICAL)) 16pygame.display.set_caption("shooting") 17 18image = pygame.image.load("C:/Users/R2D2/.spyder-py3/bbbb.png") 19aaaa = pygame.image.load("C:/Users/R2D2/.spyder-py3/bullet.png") 20 21pygame.key.set_repeat(5,5) 22position = [HORIZON/2, VERTICAL] 23 24class shot_t(): 25 def __init__(self,x,y): 26 self.x = x 27 self.y = y 28 self.flag = 0 29 30P_x = [[0 for i in range(P_MAX_BULLET)]for j in range(P_MAX_SHOT)] 31P_y = [[0 for i in range(P_MAX_BULLET)]for j in range(P_MAX_SHOT)] 32P_flag = [[0 for i in range(P_MAX_BULLET)]for j in range(P_MAX_SHOT)] 33shot_P = [[0 for i in range(P_MAX_BULLET)]for j in range(P_MAX_SHOT)] 34 35for i in range(P_MAX_SHOT): 36 for j in range(P_MAX_BULLET): 37 shot_P[i][j] = shot_t(position[0],position[1]) 38 39def main(): 40 counter = 0 41 while True: 42 screen.fill((0,0,0)) 43 44 for event in pygame.event.get(): 45 if event.type == QUIT: 46 pygame.quit() 47 sys.exit 48 49 elif event.type == KEYDOWN: 50 if event.key == K_LEFT: 51 if position[0] >= 0: 52 position[0] -= 5 53 elif event.key == K_RIGHT: 54 if position[0] <= HORIZON: 55 position[0] += 5 56 elif event.key == K_UP: 57 if position[1] >= 0: 58 position[1] -= 5 59 elif event.key == K_DOWN: 60 if position[1] <= VERTICAL: 61 position[1] += 5 62 if event.key == K_z and counter % 5 == 0: 63 for i in range(ShotLevel): 64 for j in range(P_MAX_BULLET): 65 if P_flag[i][j] == 0: 66 P_flag[i][j] = 1 67 P_x[i][j] = position[0] - 14 + positionx[i] 68 P_y[i][j] = position[1] + positiony[i] 69 break 70 71 for i in range(P_MAX_SHOT): 72 for j in range(P_MAX_BULLET): 73 if P_flag[i][j] == 1: 74 P_x[i][j] += P_sub[i] 75 P_y[i][j] -= 10 76 if P_y[i][j] < -20: 77 P_flag[i][j] = 0 78 screen.blit(aaaa, (P_x[i][j],P_y[i][j])) 79 80 rect = image.get_rect() 81 rect.center = position 82 83 screen.blit(image, rect) 84 pygame.display.update() 85 86 counter += 1 87 88if __name__ == '__main__': 89 main()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/02 05:01