Skip to content

Instantly share code, notes, and snippets.

@tbmreza
Created April 19, 2019 09:17
Show Gist options
  • Save tbmreza/1c654fbb9ec0d5c0963b572cd6c8bcb0 to your computer and use it in GitHub Desktop.
Save tbmreza/1c654fbb9ec0d5c0963b572cd6c8bcb0 to your computer and use it in GitHub Desktop.
# https://www.youtube.com/watch?v=5SlYTF2ovGg&list=PLNtxQuOxJ0p29Ry5jp3AiOJpk0UiSSlts&index=6
import pygame
# Setup
pygame.init()
clock = pygame.time.Clock()
screenSize = (500,500)
screen = pygame.display.set_mode(screenSize)
# Player data
coords = [250,0]
vel = [0,3]
acc = [0,0]
# Hazard zone
hazardCoords = [250,250]
# Main game loop
going = True
while going:
# Check if window was closed
for event in pygame.event.get():
if event.type == pygame.QUIT:
going = False
# Use arrow keys to accelerate
acc = [0,0];
keys = pygame.key.get_pressed()
if keys[pygame.K_DOWN]:
acc[1] += 1
if keys[pygame.K_UP]:
acc[1] -= 1
if keys[pygame.K_RIGHT]:
acc[0] += 1
if keys[pygame.K_LEFT]:
acc[0] -= 1
vel = [vel[0]+acc[0], vel[1]+acc[1]]
coords = [coords[0]+vel[0], coords[1]+vel[1]]
playerRect = pygame.Rect(coords[0],coords[1],50,50)
hazardRect = pygame.Rect(hazardCoords[0],hazardCoords[1],50,50)
if playerRect.colliderect(hazardRect):
going = False
# Draw graphics to screen
screen.fill((0,0,0))
pygame.draw.rect(screen,(200,180,120),playerRect,0)
pygame.draw.rect(screen,(255,0,0),hazardRect,0)
pygame.display.flip()
clock.tick(30)
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment