1

スペースインベーダーに似たゲームを作っています。射撃の頻度を下げるなどのイベントを確認する方法はありますか? 現在、スペースバーを十分に速く押すと、トップショットが画面の上部に到達する前に消えます. 2/(頂上までの所要時間)のようなショットイベントを確認できるかどうか疑問に思っていました。

これが私のコードです:

#-----!!!!SPACE INVADERS!!!!-----
import pygame, sys
from pygame.locals import *
#-----MAIN FUNCTIONS-----
def movement(move_x):
    if event.type == KEYDOWN:
        if event.key == K_LEFT:
            move_x = -5
        if event.key == K_RIGHT:
            move_x = 5
    if event.type == KEYUP:
        if event.key == K_LEFT:
            move_x = 0
        if event.key == K_RIGHT:
            move_x = 0
    return move_x


#-----FFRAME RAEE / SCREEN SIZE-----
clock = pygame.time.Clock()
w,h = 800,800
screen = pygame.display.set_mode((w,h))

#-----SETTING IMAGES-----
pygame.mouse.set_visible(0)

ship = pygame.image.load("spaceship.png")
ship = pygame.transform.scale(ship,(100,50))
ship_top = screen.get_height() - ship.get_height()
ship_left = screen.get_width()/2 - ship.get_width()/2

screen.blit(ship, (ship_left,ship_top))

shot1 = pygame.image.load("SingleBullet.png")
shot1 = pygame.transform.scale(shot1,(25,25))
shot2 = shot1
shot_count = 0
shot_y = 0
shot_y_2 = 0

#-----GLOBAL VARIABLES-----
x = 0
resetShot = 0
move_x = 0
#-----MAIN GAME LOOP-----
while True:
    clock.tick(60)
    screen.fill((0,0,0))
    #x,y = pygame.mouse.get_pos()
    screen.blit(ship, (x-ship.get_width()/2,ship_top))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        move_x = movement(move_x)

        if event.type == KEYDOWN:
            if event.key == K_SPACE and shot_count == 0:
                shot_y = h-50
                shot_x = x
            elif event.type == K_SPACE and shot_count == 1:
                shot_y_2 = h-50
                shot_x_2 = x
            print(h, ' ', shot_y, shot_count)
        if event.type == KEYUP:
            if event.key == K_SPACE and shot_count == 0:
                resetShot = 0 
            elif event.type == K_SPACE and shot_count == 1:
                resetShot = 0


    if shot_y > 0:
        screen.blit(shot1, (shot_x-shot1.get_width()/2,shot_y))
        shot_y -= 15
    if shot_y_2 > 0:
        screen.blit(shot2, (shot_x_2-shot1.get_width()/2,shot_y_2))
        shot_y_2 -= 15

    x+=move_x
    pygame.display.update()
4

2 に答える 2

2

いくつかのヒント: 最初のブリットは無意味です。while ループで既に blit を開始しているためです。

ship_left と ship_top をタプルに格納できるので、コードがすっきりします。

関数を使用し、それらに意味のある名前を付けます。これにより、あなたや他の人がコードを読みやすくなります。

もう1つ-移動関数はパラメーターを受け取りますが、それに対して何もしません。イベントを使用するため、代わりにこれを渡す必要があります。

質問に戻ります。これは、通常、このようなゲームで解決される方法です。

ミサイルのリストを作成します。各 KEYDOWN イベントは、新しいミサイルを作成し、リストに追加します。リストに 10 個のミサイルがある場合、ミサイルは作成されません。

別のクラスのミサイルを作成するのが最善です。リスト内のオブジェクトごとに、次の操作を行う必要があります。

  1. その位置に基づいて描画します。
  2. 更新 - ミサイルを上部に近づけます。
  3. ミサイルが画面の外に出ていないか確認し、出ている場合は取り除いてください。

このように、タイマーは必要なく、ショットを制限して、プレーヤーがキーボードをスパムしないようにすることができます。本当に時間に基づいて制限したい場合は、の戻り値を使用しpygame.Clock.tick()て time_since_last_shot 変数をインクリメントできます。キーを押すたびに、値が十分に大きいかどうかがチェックされ、そうであれば、変数を撃って 0 にリセットします。

于 2013-11-13T23:42:20.933 に答える