100 lines
2.5 KiB
Python
100 lines
2.5 KiB
Python
import pygame
|
|
|
|
|
|
class Particle:
|
|
def __init__(self,x,y):
|
|
self.pos = (x,y)
|
|
self.velocity = (0,0)
|
|
self.accel = (0,0)
|
|
self.surface = None
|
|
|
|
def set_surface(self,surface):
|
|
self.surface = surface
|
|
|
|
def appear(self,screen):
|
|
if self.surface is not None:
|
|
pygame.draw.circle(screen, BLUE, self.pos, CIRCLE_RADIUS)
|
|
blit_pos = (self.pos[0]-self.surface.get_width()/2, self.pos[1]-self.surface.get_height()/2)
|
|
screen.blit(self.surface, blit_pos)
|
|
|
|
def update(self):
|
|
self.velocity = (self.velocity[0]+self.accel[0], self.velocity[1]+self.accel[1])
|
|
self.pos = (self.pos[0] + self.velocity[0], self.pos[1] + self.velocity[1])
|
|
self.accel = (0,0)
|
|
|
|
def apply_force(self,force):
|
|
self.accel = (self.accel[0]+force[0],self.accel[1]+force[1])
|
|
|
|
projectiles = {}
|
|
width = 640
|
|
height = 600
|
|
SIZE = (width, height)
|
|
FRICTION = 0.3
|
|
WHITE = (255, 255, 255)
|
|
RED = (255, 0, 0)
|
|
BLUE = (0, 0, 255)
|
|
CIRCLE_RADIUS = 2
|
|
BLACK = (0, 0, 0)
|
|
|
|
def init_circle():
|
|
global particle
|
|
particle = Particle(width // 2, height // 2)
|
|
surface = pygame.Surface((CIRCLE_RADIUS * 2, CIRCLE_RADIUS * 2))
|
|
pygame.draw.circle(surface, RED, (CIRCLE_RADIUS, CIRCLE_RADIUS), CIRCLE_RADIUS)
|
|
particle.set_surface(surface.convert_alpha())
|
|
|
|
def main():
|
|
screen = init_game()
|
|
game_loop(screen)
|
|
exit_game()
|
|
|
|
def init_game():
|
|
global clock
|
|
clock = pygame.time.Clock()
|
|
pygame.init()
|
|
screen = pygame.display.set_mode(SIZE)
|
|
init_circle()
|
|
return screen
|
|
|
|
def game_loop(screen):
|
|
while True:
|
|
if not event_handling():
|
|
break
|
|
if not update_game():
|
|
break
|
|
event_handling()
|
|
update_game()
|
|
draw_screen(screen)
|
|
|
|
|
|
def event_handling():
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
return False
|
|
keys_pressed = pygame.key.get_pressed()
|
|
if keys_pressed[pygame.K_UP]:
|
|
particle.apply_force((0,-1))
|
|
if keys_pressed[pygame.K_DOWN]:
|
|
particle.apply_force((0,1))
|
|
if keys_pressed[pygame.K_LEFT]:
|
|
particle.apply_force((-1,0))
|
|
if keys_pressed[pygame.K_RIGHT]:
|
|
particle.apply_force((1,0))
|
|
particle.apply_force((particle.velocity[0]*-FRICTION,particle.velocity[1]*-FRICTION))
|
|
particle.update()
|
|
return True
|
|
|
|
def update_game():
|
|
clock.tick(60)
|
|
return True
|
|
|
|
def draw_screen(screen):
|
|
screen.fill(BLACK)
|
|
particle.appear(screen)
|
|
pygame.display.flip()
|
|
|
|
def exit_game():
|
|
pygame.quit()
|
|
|
|
main()
|