Untitled

Run Settings
LanguagePython
Language Version
Run Command
import sys import random import pygame from pygame.locals import * SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) BG_COLOR = (115, 68, 10) PLAYER_COLOR = (130, 124, 105) ENEMY_COLOR = (255, 0, 0) class Sprite(pygame.sprite.Sprite): def __init__(self, width, height, color): self.surf = pygame.Surface((width, height)) self.surf.fill(color) def draw(self): screen.blit(self.surf, self.rect) class Player(Sprite): def __init__(self): super().__init__(48, 24, PLAYER_COLOR) self.rect = self.surf.get_rect() self.speed = 1 def update(self, pressed_keys): if pressed_keys[K_LEFT]: self.rect.move_ip(-self.speed, 0) if pressed_keys[K_RIGHT]: self.rect.move_ip(self.speed, 0) if pressed_keys[K_UP]: self.rect.move_ip(0, -self.speed) if pressed_keys[K_DOWN]: self.rect.move_ip(0, self.speed) self.keep_onscreen() def keep_onscreen(self): if self.rect.left < 0: self.rect.left = 0 if self.rect.right > SCREEN_WIDTH: self.rect.right = SCREEN_WIDTH if self.rect.top < 0: self.rect.top = 0 if self.rect.bottom > SCREEN_HEIGHT: self.rect.bottom = SCREEN_HEIGHT class Enemy(Sprite): def __init__(self): super().__init__(24, 24, ENEMY_COLOR) self.rect = self.surf.get_rect( center=( random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100), random.randint(0, SCREEN_HEIGHT), ) ) self.speed = random.randint(1, 2) def update(self): self.rect.move_ip(-self.speed, 0) if self.rect.right < 0: self.kill() def loop(): sprites = pygame.sprite.Group() player = Player() sprites.add(player) while True: # get events for e in pygame.event.get(): if e.type == QUIT: return pressed_keys = pygame.key.get_pressed() # update player.update(pressed_keys) # draw screen.fill(BG_COLOR) for s in sprites: s.draw() pygame.display.flip() def main(): pygame.init() loop() pygame.quit() if __name__ == '__main__': main()
Editor Settings
Theme
Key bindings
Full width
Lines