Skip to content

Instantly share code, notes, and snippets.

@mjnguyen
Created October 4, 2017 01:29
Show Gist options
  • Save mjnguyen/061c662a11f99929de0ae5d6d505c3db to your computer and use it in GitHub Desktop.
Save mjnguyen/061c662a11f99929de0ae5d6d505c3db to your computer and use it in GitHub Desktop.
move the player
import pygame
import random
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
class Block(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super(Block, self).__init__()
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
pygame.init()
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
all_sprites_list = pygame.sprite.Group()
clock = pygame.time.Clock()
for i in range(10):
# This represents a block
block = Block(BLACK, 20, 15)
# Set a random location for the block
block.rect.x = random.randrange(screen_width)
block.rect.y = random.randrange(screen_height)
all_sprites_list.add(block)
# Create a RED player block
player = Block(RED, 20, 15)
all_sprites_list.add(player)
done = False
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pos = pygame.mouse.get_pos()
player.rect.x = pos[0]
player.rect.y = pos[1]
# Clear the screen
screen.fill(WHITE)
# Draw all the spites
all_sprites_list.draw(screen)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 60 frames per second
clock.tick(60)
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment