Skip to content

Instantly share code, notes, and snippets.

@ritiek
Last active February 26, 2023 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritiek/0fec92455fd6568be4e8767f0f1d3261 to your computer and use it in GitHub Desktop.
Save ritiek/0fec92455fd6568be4e8767f0f1d3261 to your computer and use it in GitHub Desktop.
A simple (py)game
import pygame
import random
width, height = (450, 600)
background_color = (10,10,10)
clock = pygame.time.Clock()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Falling Blocks")
pygame.display.flip()
hero_block = pygame.rect.Rect((width/2-30,height-2*60,60,60))
running = True
enemy_block = pygame.rect.Rect((random.randint(0,width-60),-70,60,60))
enemy_block_attrs = {"speed": random.randint(5, 11),
"color": (random.randint(40,230),
random.randint(40,230),
random.randint(40,230))
}
while running:
if enemy_block.topleft[1] > height or enemy_block.topright[1] > height:
enemy_block = pygame.rect.Rect((random.randint(0,width-60),-70,60,60))
enemy_block_attrs = {"speed": random.randint(5, 11),
"color": (random.randint(40,230),
random.randint(40,230),
random.randint(40,230))
}
screen.fill(background_color)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
hero_block.move_ip(-4, 0)
if key[pygame.K_RIGHT]:
hero_block.move_ip(4, 0)
if key[pygame.K_UP]:
hero_block.move_ip(0, -4)
if key[pygame.K_DOWN]:
hero_block.move_ip(0, 4)
enemy_block.move_ip(0, enemy_block_attrs["speed"])
pygame.draw.rect(screen, enemy_block_attrs["color"], enemy_block)
pygame.draw.rect(screen, (220,220,220), hero_block)
pygame.display.update()
if hero_block.colliderect(enemy_block):
# TODO: Show total seconds survived
exit()
clock.tick(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment