Untitled
unknown
plain_text
a year ago
3.0 kB
9
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))
goal = pygame.Rect((250,200,50,50))
bullets = []
running = True
direction = 'U'
bullet_created = time.time()
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):
for bullet in bullets:
bullet_name = bullet['name']
direction = bullet['direction']
bullet_loc = bullet['location']
if direction == 'U':
bullet_name.move_ip(0,-2)
elif direction == 'D':
bullet_name.move_ip(0,2)
elif direction == 'R':
bullet_name.move_ip(2,0)
elif direction == 'L':
bullet_name.move_ip(-2,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)
while running:
screen.fill((0,0,0))
pygame.draw.rect(screen,(0,0,255),player)
pygame.draw.rect(screen,(0,255,0),goal)
if player.colliderect(goal):
new_x = random.randrange(25,screen_width-25)
new_y = random.randrange(25,screen_height-25)
goal.center = (new_x,new_y)
key = pygame.key.get_pressed()
if key[pygame.K_a] == True:
player.move_ip(-1,0)
direction = 'L'
elif key[pygame.K_d] == True:
player.move_ip(1,0)
direction = 'R'
elif key[pygame.K_w] == True:
player.move_ip(0,-1)
direction = 'U'
elif key[pygame.K_s] == True:
player.move_ip(0,1)
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)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
pygame.quit()Editor is loading...
Leave a Comment