Skip to content

Instantly share code, notes, and snippets.

@mhansen
Created May 13, 2012 05:50
Show Gist options
  • Save mhansen/2685815 to your computer and use it in GitHub Desktop.
Save mhansen/2685815 to your computer and use it in GitHub Desktop.
import pygame
# Game variables
my_dudes_x_position = 300
bullets = []
window = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
def clear_the_screen():
pygame.draw.rect(window, (0,0,0), (0,0,640,480))
def draw_my_dude():
pygame.draw.rect(window, (0, 255, 0), (my_dudes_x_position - 30, 400, 60, 20))
def draw_bullets():
for bullet in bullets:
pygame.draw.rect(window, (255, 0, 0), (bullet[0] - 2, bullet[1] - 2, 4, 4))
def draw_everything():
clear_the_screen()
draw_my_dude()
draw_bullets()
pygame.display.flip()
def move_bullets():
for bullet in bullets:
bullet[1] = bullet[1] - 1
running = True
while running:
clock.tick(120)
move_bullets()
draw_everything()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
if event.key == pygame.K_LEFT:
my_dudes_x_position -= 15
if event.key == pygame.K_RIGHT:
my_dudes_x_position += 15
if event.key == pygame.K_SPACE:
bullets.append([my_dudes_x_position, 380])
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment