Created
April 22, 2026 22:49
-
-
Save jsbueno/52d4098fcda613b48817d672cb60eac9 to your computer and use it in GitHub Desktop.
"red square moving" on screen: example during 2026-4-22 livestream
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 | |
| tela = pygame.display.set_mode((800, 600)) | |
| class Quadrado: | |
| larg = 50 | |
| alt = 50 | |
| cor = (255, 0, 0) | |
| fundo = (255, 255, 255) | |
| def __init__(self, superficie, x=None, y=None): | |
| self.superficie = superficie | |
| if x is None: | |
| x, y = 0, 0 | |
| self.x = x | |
| self.y = y | |
| def update(self): | |
| pygame.draw.rect(self.superficie, self.cor, (self.x, self.y, self.larg, self.alt)) | |
| @property | |
| def x(self): | |
| return self._x | |
| @x.setter | |
| def x(self, valor): | |
| if 0 <= valor <= self.superficie.width - self.larg: | |
| self._x = valor | |
| @property | |
| def y(self): | |
| return self._y | |
| @y.setter | |
| def y(self, valor): | |
| if 0 <= valor <= self.superficie.height - self.alt: | |
| self._y = valor | |
| q = Quadrado(tela) | |
| q1 = Quadrado(tela) | |
| q1.cor = (0, 0, 255) | |
| def update(superficie, *objetos, fundo=(255,255,255) ): | |
| superficie.fill(fundo) | |
| for objeto in objetos: | |
| objeto.update() | |
| pygame.display.update() | |
| def principal(): | |
| while True: | |
| teclas = pygame.key.get_pressed() | |
| if teclas[pygame.K_UP]: | |
| q.y -= 10 | |
| if teclas[pygame.K_DOWN]: | |
| q.y += 10 | |
| if teclas[pygame.K_RIGHT]: | |
| q.x += 10 | |
| if teclas[pygame.K_LEFT]: | |
| q.x -= 10 | |
| update(tela, q, q1) | |
| pygame.time.delay(30) | |
| pygame.event.pump() | |
| if __name__ == "__main__": | |
| principal() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment