from typing import Dict import pygame, random #import pygame.mixer pygame.init() #all the variables displaysize, playersize = (1000,750), (100,100) level = 1 defaultborders = True playerX, playerY = 250, 250 oldX, oldY = playerX, playerY playerXc, playerYc = 0, 0 randomblock = pygame.Surface((100, 100)) randomblock.fill((0, 0, 255)) blocks = [ #(x, y, w, h) (420, 400, 100, 100) ] #all the pygame setups and shit screen = pygame.display.set_mode(displaysize) player = pygame.image.load('player.png') player = pygame.transform.scale(player, playersize) player_rect = player.get_rect() #defines borders def def_borders(x,y, b1, b2, b3, b4): if x < b1: x = 1 elif y > b2: y = 649 if x > b3: x = 899 elif y < b4: y = 1 return x, y def check_collision(x1, y1, w1, h1, x2, y2, w2, h2): """ Returns True if the two rectangles overlap. Works like pygame.Rect.colliderect. x1, y1 = top-left of first rectangle w1, h1 = width/height of first rectangle x2, y2 = top-left of second rectangle w2, h2 = width/height of second rectangle """ return (x1 < x2 + w2 and x1 + w1 > x2 and y1 < y2 + h2 and y1 + h1 > y2) pygame.display.set_caption("UNDERTALE", icontitle="Inspired by Toby Fox <3") pygame.display.set_icon("icon.png") paused = False running = True while running == True: oldX, oldY = playerX, playerY screen.fill((0, 0, 0)) playerY += playerYc playerX += playerXc for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit() if event.key == pygame.K_SPACE: print("playerX="+str(playerX)+", playerY="+str(playerY)) if event.key == pygame.K_LEFT or event.key == pygame.K_a: #if randomblock not in player.colliderect(): playerXc = -1 if event.key == pygame.K_RIGHT or event.key == pygame.K_d: playerXc = 1 if event.key == pygame.K_UP or event.key == pygame.K_w: playerYc = -1 if event.key == pygame.K_DOWN or event.key == pygame.K_s: playerYc = 1 if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_a: playerXc = 0 if event.key == pygame.K_RIGHT or event.key == pygame.K_d: playerXc = 0 if event.key == pygame.K_UP or event.key == pygame.K_w: playerYc = 0 if event.key == pygame.K_DOWN or event.key == pygame.K_s: playerYc = 0 if defaultborders: playerX, playerY = def_borders(playerX, playerY, 0, 650, 900, 0) for bx, by, bw, bh in blocks: if check_collision(playerX, playerY, 100, 100, bx, by, bw, bh): playerX, playerY = oldX, oldY break # stop checking after first collision screen.blit(randomblock, (420, 400)) screen.blit(player, (playerX, playerY)) #print() clock = pygame.time.Clock() pygame.display.update()