Update controls.py
parent
19089824f9
commit
66c81fe557
222
controls.py
222
controls.py
|
@ -1,112 +1,112 @@
|
|||
import pygame, sys
|
||||
import time
|
||||
from bullet import Bullet
|
||||
from enemy import Enemy
|
||||
|
||||
def events(gun, screen, bullets):
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
sys.exit()
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_RIGHT:
|
||||
gun.mright = True
|
||||
elif event.key == pygame.K_LEFT:
|
||||
gun.mleft = True
|
||||
elif event.key == pygame.K_SPACE:
|
||||
new_bullet = Bullet(screen, gun)
|
||||
bullets.add(new_bullet)
|
||||
elif event.type == pygame.KEYUP:
|
||||
if event.key == pygame.K_RIGHT:
|
||||
gun.mright = False
|
||||
elif event.key == pygame.K_LEFT:
|
||||
gun.mleft = False
|
||||
|
||||
|
||||
def update(bg_color, screen, gun, enemies, bullets, stats, scores):
|
||||
screen.fill(bg_color)
|
||||
gun.output()
|
||||
enemies.draw(screen)
|
||||
scores.score_output()
|
||||
for bullet in bullets.sprites():
|
||||
bullet.draw_bullet()
|
||||
pygame.display.flip()
|
||||
|
||||
def update_bullets(bullets, enemies, screen, stats, scores):
|
||||
bullets.update()
|
||||
for bullet in bullets.copy():
|
||||
if bullet.rect.bottom <= 0:
|
||||
bullets.remove(bullet)
|
||||
collisions=pygame.sprite.groupcollide(bullets, enemies, True, True)
|
||||
if collisions:
|
||||
for enemies in collisions.values():
|
||||
stats.score += 10 * len(enemies)
|
||||
scores.image_score()
|
||||
high_score_check(stats, scores)
|
||||
scores.image_guns()
|
||||
if len(enemies)==0:
|
||||
bullets.empty()
|
||||
create_army(screen, enemies)
|
||||
|
||||
|
||||
|
||||
|
||||
def create_army(screen, enemies):
|
||||
enemy = Enemy(screen)
|
||||
enemy_width = enemy.rect.width
|
||||
enemy_height = enemy.rect.height
|
||||
number_enemy_y = int((500 - 100 - 2 * enemy_height)/ enemy_height)
|
||||
number_enemy_x = int((500 -2 * enemy_width)/ enemy_width)
|
||||
|
||||
for j in range (number_enemy_y):
|
||||
for i in range(number_enemy_x):
|
||||
enemy = Enemy(screen)
|
||||
enemy.rect.x = enemy_width + enemy_width * i
|
||||
enemy.y = enemy_height + enemy_height * j
|
||||
enemy.rect.y = enemy.y
|
||||
enemies.add(enemy)
|
||||
|
||||
|
||||
|
||||
def update_enemies(stats,screen, gun, enemies, bullets, scores):
|
||||
enemies.update()
|
||||
if pygame.sprite.spritecollideany(gun, enemies):
|
||||
gun_kill(stats,screen, gun, enemies, bullets, scores)
|
||||
enemies_check(stats,screen, gun, enemies, bullets, scores)
|
||||
|
||||
|
||||
|
||||
|
||||
def gun_kill(stats,screen, gun, enemies, bullets, scores):
|
||||
if stats.guns_left>0:
|
||||
stats.guns_left -= 1
|
||||
scores.image_guns()
|
||||
enemies.empty()
|
||||
bullets.empty()
|
||||
create_army(screen, enemies)
|
||||
gun.create_gun()
|
||||
time.sleep(2)
|
||||
else:
|
||||
stats.run_game==False
|
||||
sys.exit()
|
||||
|
||||
|
||||
def enemies_check(stats,screen, gun, enemies, bullets, scores):
|
||||
screen_rect = screen.get_rect()
|
||||
for enemy in enemies.sprites():
|
||||
if enemy.rect.bottom >= screen_rect.bottom:
|
||||
gun_kill(stats,screen, gun, enemies, bullets, scores)
|
||||
break
|
||||
|
||||
def high_score_check(stats, scores):
|
||||
if stats.score >= stats.high_score:
|
||||
stats.high_score = stats.score
|
||||
scores.image_high_score()
|
||||
with open("highscore.txt", "w") as f:
|
||||
f.write(str(stats.high_score))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
import pygame, sys
|
||||
import time
|
||||
from bullet import Bullet
|
||||
from enemy import Enemy
|
||||
|
||||
def events(gun, screen, bullets):
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
sys.exit()
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_RIGHT:
|
||||
gun.mright = True
|
||||
elif event.key == pygame.K_LEFT:
|
||||
gun.mleft = True
|
||||
elif event.key == pygame.K_SPACE:
|
||||
new_bullet = Bullet(screen, gun)
|
||||
bullets.add(new_bullet)
|
||||
elif event.type == pygame.KEYUP:
|
||||
if event.key == pygame.K_RIGHT:
|
||||
gun.mright = False
|
||||
elif event.key == pygame.K_LEFT:
|
||||
gun.mleft = False
|
||||
|
||||
|
||||
def update(bg_color, screen, gun, enemies, bullets, stats, scores):
|
||||
screen.fill(bg_color)
|
||||
gun.output()
|
||||
enemies.draw(screen)
|
||||
scores.score_output()
|
||||
for bullet in bullets.sprites():
|
||||
bullet.draw_bullet()
|
||||
pygame.display.flip()
|
||||
|
||||
def update_bullets(bullets, enemies, screen, stats, scores):
|
||||
bullets.update()
|
||||
for bullet in bullets.copy():
|
||||
if bullet.rect.bottom <= 0:
|
||||
bullets.remove(bullet)
|
||||
collisions=pygame.sprite.groupcollide(bullets, enemies, True, True)
|
||||
if collisions:
|
||||
for enemies in collisions.values():
|
||||
stats.score += 10 * len(enemies)
|
||||
scores.image_score()
|
||||
high_score_check(stats, scores)
|
||||
scores.image_guns()
|
||||
if len(enemies)==0:
|
||||
bullets.empty()
|
||||
create_army(screen, enemies)
|
||||
|
||||
|
||||
|
||||
|
||||
def create_army(screen, enemies):
|
||||
enemy = Enemy(screen)
|
||||
enemy_width = enemy.rect.width
|
||||
enemy_height = enemy.rect.height
|
||||
number_enemy_y = int((500 - 100 - 2 * enemy_height)/ enemy_height)
|
||||
number_enemy_x = int((500 -2 * enemy_width)/ enemy_width)
|
||||
|
||||
for j in range (number_enemy_y):
|
||||
for i in range(number_enemy_x):
|
||||
enemy = Enemy(screen)
|
||||
enemy.rect.x = enemy_width + enemy_width * i
|
||||
enemy.y = enemy_height + enemy_height * j
|
||||
enemy.rect.y = enemy.y
|
||||
enemies.add(enemy)
|
||||
|
||||
|
||||
|
||||
def update_enemies(stats,screen, gun, enemies, bullets, scores):
|
||||
enemies.update()
|
||||
if pygame.sprite.spritecollideany(gun, enemies):
|
||||
gun_kill(stats,screen, gun, enemies, bullets, scores)
|
||||
enemies_check(stats,screen, gun, enemies, bullets, scores)
|
||||
|
||||
|
||||
|
||||
|
||||
def gun_kill(stats,screen, gun, enemies, bullets, scores):
|
||||
if stats.guns_left>0:
|
||||
stats.guns_left -= 1
|
||||
scores.image_guns()
|
||||
enemies.empty()
|
||||
bullets.empty()
|
||||
create_army(screen, enemies)
|
||||
gun.create_gun()
|
||||
time.sleep(2)
|
||||
else:
|
||||
stats.run_game==False
|
||||
sys.exit()
|
||||
|
||||
|
||||
def enemies_check(stats,screen, gun, enemies, bullets, scores):
|
||||
screen_rect = screen.get_rect()
|
||||
for enemy in enemies.sprites():
|
||||
if enemy.rect.bottom >= screen_rect.bottom:
|
||||
gun_kill(stats,screen, gun, enemies, bullets, scores)
|
||||
break
|
||||
|
||||
def high_score_check(stats, scores):
|
||||
if stats.score >= stats.high_score:
|
||||
stats.high_score = stats.score
|
||||
scores.image_high_score()
|
||||
with open("highscores.txt", "w") as f:
|
||||
f.write(str(stats.high_score))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue