Created
June 4, 2017 16:04
-
-
Save triton11/b8db777e56f84e3d2d79b99a0514a8de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import random | |
BLACK = ( 0, 0, 0) | |
WHITE = (255, 255, 255) | |
RED = (255, 0, 0) | |
class Player(pygame.sprite.Sprite): | |
def __init__(self, color, width, height): | |
super().__init__() | |
self.image = pygame.Surface([width, height]) | |
self.image.fill(color) | |
self.rect = self.image.get_rect() | |
class Block(pygame.sprite.Sprite): | |
def __init__(self, color, width, height): | |
super().__init__() | |
self.image = pygame.Surface([width, height]) | |
self.image.fill(color) | |
self.image.set_colorkey(WHITE) | |
self.rect = self.image.get_rect() | |
class Bad_block(pygame.sprite.Sprite): | |
def __init__(self, color, width, height): | |
super().__init__() | |
self.image = pygame.Surface([width, height]) | |
self.image.fill(color) | |
self.image.set_colorkey(WHITE) | |
self.rect = self.image.get_rect() | |
pygame.init() | |
BLACK = ( 0, 0, 0) | |
WHITE = (255, 255, 255) | |
RED = (255, 0, 0) | |
GREEN = ( 0, 255, 0) | |
BLUE = ( 0, 0, 255) | |
screen_width = 700 | |
screen_height = 500 | |
screen = pygame.display.set_mode([screen_width, screen_height]) | |
pygame.display.set_caption("Block game") | |
block_list = pygame.sprite.Group() | |
bb_list = pygame.sprite.Group() | |
all_sprites_list = pygame.sprite.Group() | |
for i in range(20): | |
block = Block(BLACK, 20, 20) | |
block.rect.x = random.randrange(screen_width) | |
block.rect.y = random.randrange(screen_height) | |
block_list.add(block) | |
all_sprites_list.add(block) | |
for i in range(20): | |
block = Bad_block(RED, 20, 20) | |
block.rect.x = random.randrange(screen_width) | |
block.rect.y = random.randrange(screen_height) | |
bb_list.add(block) | |
all_sprites_list.add(block) | |
player = Player(GREEN ,20, 20) | |
player.rect.x = 350 | |
player.rect.y = 250 | |
all_sprites_list.add(player) | |
done = False | |
change_x = 0 | |
change_y = 0 | |
#good_sound = pygame.mixer.Sound("laser1.wav") | |
#bad_sound = pygame.mixer.Sound("laser2.wav") | |
clock = pygame.time.Clock() | |
score = 0 | |
end = False | |
while not done: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
done = True | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_LEFT: | |
change_x = (-3) | |
if event.key == pygame.K_RIGHT: | |
change_x = (3) | |
if event.key == pygame.K_UP: | |
change_y = (-3) | |
if event.key == pygame.K_DOWN: | |
change_y = (3) | |
if event.type == pygame.KEYUP: | |
if event.key == pygame.K_LEFT: | |
change_x = 0 | |
if event.key == pygame.K_RIGHT: | |
change_x = 0 | |
if event.key == pygame.K_UP: | |
change_y = 0 | |
if event.key == pygame.K_DOWN: | |
change_y = 0 | |
#Notice this is indented just once | |
if len(block_list) == 0: | |
end = True | |
player.rect.x += change_x | |
player.rect.y += change_y | |
if player.rect.x > 680: | |
player.rect.x = 680 | |
if player.rect.x < 0: | |
player.rect.x = 0 | |
if player.rect.y > 480: | |
player.rect.y = 480 | |
if player.rect.y < 0: | |
player.rect.y = 0 | |
screen.fill(WHITE) | |
blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True) | |
bb_hit_list = pygame.sprite.spritecollide(player, bb_list, True) | |
for block in blocks_hit_list: | |
#good_sound.play() | |
score += 1 | |
print(score) | |
for bb in bb_hit_list: | |
#bad_sound.play() | |
score -=1 | |
print(score) | |
all_sprites_list.draw(screen) | |
font = pygame.font.SysFont('Calibri', 25, True, False) | |
text = font.render(str(score), True, GREEN) | |
screen.blit(text, [0,0]) | |
if end == True: | |
screen.fill (BLACK) | |
font = pygame.font.SysFont('Calibri', 25, True, False) | |
text = font.render(str(score), True, GREEN) | |
screen.blit(text, [250,250]) | |
done = True | |
pygame.display.flip() | |
clock.tick(60) | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment