Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Now we can shoot by pressing spacebar. #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions gameRole.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ def __init__(self, plane_img, player_rect, init_pos):
self.bullets = pygame.sprite.Group() # 玩家飞机所发射的子弹的集合
self.img_index = 0 # 玩家精灵图片索引
self.is_hit = False # 玩家是否被击中
self.is_gun_ready = True

def shoot(self, bullet_img):
bullet = Bullet(bullet_img, self.rect.midtop)
self.bullets.add(bullet)
self.is_gun_ready = False

def reload(self):
self.is_gun_ready = True

def moveUp(self):
if self.rect.top <= 0:
Expand Down
9 changes: 6 additions & 3 deletions mainGame.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@

# 控制发射子弹频率,并发射子弹
if not player.is_hit:
if shoot_frequency % 15 == 0:
bullet_sound.play()
player.shoot(bullet_img)
if shoot_frequency % 10 == 0:
player.reload()
shoot_frequency += 1
if shoot_frequency >= 15:
shoot_frequency = 0
Expand Down Expand Up @@ -179,6 +178,10 @@
player.moveLeft()
if key_pressed[K_d] or key_pressed[K_RIGHT]:
player.moveRight()
if key_pressed[K_SPACE] or key_pressed[K_SPACE]:
if player.is_gun_ready:
bullet_sound.play()
player.shoot(bullet_img)


font = pygame.font.Font(None, 48)
Expand Down