import pygame import random # Pygame setup pygame.init() # Note: pygame.mixer.init was called by this screen = pygame.display.set_mode((800, 600)) done = False font = pygame.font.SysFont("Courier New", 16) clock = pygame.time.Clock() # Create some game variables player = [100, 300, 100.0, 25, 0.0] # x, y, speed, radius, cooldown-timer #bullet_x = [] #bullet_y = [] #bullet_speed = [] # -or- bullet_list = [] # Each element is a sub-list [x, y, speed, radius] target = [500, 300, 50.0, 150] # x, y, speed, radius sounds = {"ouch" : [], "pow": []} sounds["pow"].append(pygame.mixer.Sound("bullet1.wav")) sounds["pow"].append(pygame.mixer.Sound("bullet2.WAV")) sounds["pow"].append(pygame.mixer.Sound("bullet3.WAV")) sounds["ouch"].append(pygame.mixer.Sound("ouch1.wav")) sounds["ouch"].append(pygame.mixer.Sound("ouch2.wav")) # GAME LOOP while not done: # INPUT dt = clock.tick() / 1000.0 evt = pygame.event.poll() if evt.type == pygame.KEYUP and evt.key == pygame.K_SPACE: player[4] = 0.0 key_pressed = pygame.key.get_pressed() if key_pressed[pygame.K_ESCAPE]: done = True if key_pressed[pygame.K_w]: dy = -1 elif key_pressed[pygame.K_s]: dy = 1 else: dy = 0 if key_pressed[pygame.K_SPACE] and player[4] <= 0: brad = random.randint(3, 8) bx = int(player[0] + player[3] + brad) by = int(player[1]) bspeed = random.randint(50, 150) bullet_list.append([bx, by, bspeed, brad]) player[4] = 0.5 # Play a pow sound! snd = random.choice(sounds["pow"]) snd.play() # UPDATES player[1] += dy * player[2] * dt target[1] += target[2] * dt player[4] -= dt # Cooldown timer if target[1] > 600 + target[3]: target[1] = -target[3] # ... move all bullets for b in bullet_list: b[0] += b[2] * dt adist = target[0] - b[0] bdist = target[1] - b[1] dist = (adist ** 2 + bdist ** 2) ** 0.5 if dist <= target[3] + b[3] and target[3] > 5: target[3] -= b[3] bullet_list.remove(b) # Play an ouch soudn snd = random.choice(sounds["ouch"]) snd.play() break elif b[0] > screen.get_width() + b[3]: bullet_list.remove(b) # DRAWING screen.fill((0,0,0)) # ... draw the player px = int(player[0]) py = int(player[1]) pr = int(player[3]) pygame.draw.circle(screen, (255,0,0), (px, py), pr) # ... draw the bullets i = 0 while i < len(bullet_list): b = bullet_list[i] # b is a sub-list [x, y, speed, radius] bx = int(b[0]) by = int(b[1]) br = int(b[3]) pygame.draw.circle(screen, (255, 255, 0), (bx, by), br) i += 1 # ... draw the target tx = int(target[0]) ty = int(target[1]) tr = int(target[3]) pygame.draw.circle(screen, (0, 255, 0), (tx, ty), tr) # ... draw some stats text = "#bullets: " + str(len(bullet_list)) screen.blit(font.render(text, False, (255,255,255)), (0, 0)) text = "fps: " + str(int(clock.get_fps())) screen.blit(font.render(text, False, (255, 255, 255)), (0, 20)) pygame.display.flip() # Pygame shutdown pygame.quit() # Note: pygame.mixer.quit was called by this