Untitled
unknown
plain_text
a year ago
4.9 kB
7
Indexable
import pygame import random import time screen_height = 800 screen_width = 1200 screen = pygame.display.set_mode((screen_width,screen_height)) player = pygame.Rect(300,250,50,50) enemy = pygame.Rect(600, 400, 50, 50) bullets = [] running = True direction = 'U' bullet_created = time.time() ai_bullet_created = time.time() enemy_lives = 3 player_lives = 3 def create_bullet(direction,box): global bullet_num, bullets bullet_direction = direction switch = False if direction == 'U': x,y = box.midtop x -= 2.5 y -= 7.5 switch = True bullet_loc = {'x': x, 'y': y} elif direction == 'D': x,y = box.midbottom x -= 2.5 switch = True bullet_loc = {'x': x, 'y': y} elif direction == 'R': x,y = box.midright bullet_loc = {'x': x, 'y': y} elif direction == 'L': x,y = box.midleft x -= 7.5 y -= 2.5 bullet_loc = {'x': x, 'y': y} if switch: bullet_name = pygame.Rect(x,y,5,10) else: bullet_name = pygame.Rect(x,y,10,5) bullet = { 'name': bullet_name, 'direction': bullet_direction, 'location': bullet_loc } bullets.append(bullet) def update_bullets(bullets): global running, player_lives, enemy_lives for bullet in bullets: bullet_name = bullet['name'] direction = bullet['direction'] bullet_loc = bullet['location'] if direction == 'U': bullet_name.move_ip(0,-3) elif direction == 'D': bullet_name.move_ip(0,3) elif direction == 'R': bullet_name.move_ip(3,0) elif direction == 'L': bullet_name.move_ip(-3,0) if not (bullet_loc['x'] < 0 or bullet_loc['y'] < 0 or bullet_loc['x'] > screen_width or bullet_loc['y'] > screen_width): pygame.draw.rect(screen,(255,255,255),bullet_name) else: bullets.remove(bullet) if bullet_name.colliderect(player): player_lives -= 1 bullets.remove(bullet) if bullet_name.colliderect(enemy): enemy_lives -= 1 bullets.remove(bullet) def enemy_update(player,enemy,current_time): global ai_bullet_created x, y = player.center x = random.randrange(-500,500) + x y = random.randrange(-500,500) + y myx, myy = enemy.center diff_x = abs(x-myx) diff_y = abs(y-myy) if diff_x > diff_y: actudiff_y = y - myy if actudiff_y < 0: enemy.move_ip(0,-1) elif actudiff_y > 0: enemy.move_ip(0,1) elif (current_time - ai_bullet_created) > 0.5: ai_bullet_created = time.time() actudiff_x = x - myx if actudiff_x < 0: create_bullet('L',enemy) elif actudiff_x > 0: create_bullet('R',enemy) if diff_x < diff_y: actudiff_x = x - myx if actudiff_x < 0: enemy.move_ip(-1,0) elif actudiff_x > 0: enemy.move_ip(1,0) elif (current_time - ai_bullet_created) > 0.5: ai_bullet_created = time.time() actudiff_y = y - myy if actudiff_y < 0: create_bullet('U',enemy) elif actudiff_y > 0: create_bullet('D',enemy) def check_outside(box): global screen_width, screen_height x,y = box.center if x < 0: box.move_ip(5,0) if x > screen_width: box.move_ip(-5,0) if y < 0: box.move_ip(0,5) if y > screen_height: box.move_ip(0,-5) while running: screen.fill((0,0,0)) pygame.draw.rect(screen,(0,0,255),player) pygame.draw.rect(screen,(255,0,0),enemy) key = pygame.key.get_pressed() if key[pygame.K_a] == True: player.move_ip(-2,0) direction = 'L' elif key[pygame.K_d] == True: player.move_ip(2,0) direction = 'R' elif key[pygame.K_w] == True: player.move_ip(0,-2) direction = 'U' elif key[pygame.K_s] == True: player.move_ip(0,2) direction = 'D' current_time = time.time() if key[pygame.K_SPACE] == True and (current_time - bullet_created) > 0.5: create_bullet(direction,player) bullet_created = time.time() update_bullets(bullets) if enemy_lives >= 0: enemy_update(player,enemy,current_time) else: running = False print('You Won!') if player_lives <= 0: running = False print('You Lost.') check_outside(player) check_outside(enemy) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.display.flip() pygame.quit()
Editor is loading...
Leave a Comment