Created
November 21, 2020 13:43
-
-
Save Keshizin/51f3aea2945f2c2b8d2bed2b6be06cbb 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, sys, os | |
from pygame.locals import * | |
from sys import exit | |
# Inicializando todos os módulos de PyGame | |
pygame.init() | |
# Resolução da janela do jogo | |
WINDOW_WIDTH_SCREEN = 640 | |
WINDOW_HEIGHT_SCREEN = 480 | |
BLACK = (0, 0, 0) | |
RED = (255, 0, 0) | |
player_pos = [50, 50] | |
player_color = BLACK | |
player_collision_rect = pygame.Rect(player_pos[0], player_pos[1], 100, 100) | |
enemy_pos = [200, 200] | |
enemy_collision_rect = pygame.Rect(enemy_pos[0], enemy_pos[1], 50, 50) | |
keys = [False, False, False, False] | |
# ----------------------------------------------------------------------------- | |
# MAIN | |
# ----------------------------------------------------------------------------- | |
def main(): | |
clock = pygame.time.Clock() | |
screen = pygame.display.set_mode((WINDOW_WIDTH_SCREEN, WINDOW_HEIGHT_SCREEN), 0, 32) | |
global player_collision_rect | |
while True: | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
pygame.quit() | |
sys.exit() | |
if event.type == KEYDOWN: | |
if event.key == K_LEFT: | |
keys[0] = True | |
if event.key == K_RIGHT: | |
keys[1] = True | |
if event.key == K_UP: | |
keys[2] = True | |
if event.key == K_DOWN: | |
keys[3] = True | |
if event.type == KEYUP: | |
if event.key == K_LEFT: | |
keys[0] = False | |
if event.key == K_RIGHT: | |
keys[1] = False | |
if event.key == K_UP: | |
keys[2] = False | |
if event.key == K_DOWN: | |
keys[3] = False | |
move = [0, 0] | |
if keys[0]: | |
player_pos[0] = player_pos[0] - 5 | |
if keys[1]: | |
player_pos[0] = player_pos[0] + 5 | |
if keys[2]: | |
player_pos[1] = player_pos[1] - 5 | |
if keys[3]: | |
player_pos[1] = player_pos[1] + 5 | |
# --------------------------------------------------------------------- | |
# TESTE DE COLISÃO DE OBJETOS | |
# --------------------------------------------------------------------- | |
player_collision_rect.x = int(player_pos[0]) | |
player_collision_rect.y = int(player_pos[1]) | |
# SE TEVE COLISÃO - MUDAR A COR DO PLAYER PRA VERMELHO | |
if player_collision_rect.colliderect(enemy_collision_rect): | |
player_color = RED | |
else: | |
player_color = BLACK | |
# --------------------------------------------------------------------- | |
# LIMPA O FRAME | |
# --------------------------------------------------------------------- | |
screen.fill((255, 255, 255)) | |
# --------------------------------------------------------------------- | |
# DESENHA OS OBJETOS | |
# --------------------------------------------------------------------- | |
pygame.draw.rect(screen, player_color, (player_pos[0], player_pos[1], 100, 100)) | |
pygame.draw.rect(screen, BLACK, (enemy_pos[0], enemy_pos[1], 50, 50)) | |
pygame.display.flip() | |
clock.tick(60) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment