# Pygame template -skeleton for a new pygame project import pygame import random WIDTH = 800 HEIGHT = 600 FPS = 30 # define colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) class Player (pygame.sprite.Sprite): # sprite for the player def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((50, 50)) self.image.fill(GREEN) self.rect = self.image.get_rect() self.rect.center = (WIDTH / 2, HEIGHT / 2) def update(self): self.rect.x += 5 if self.rect.left > WIDTH: self.rect.right = 0 # initalize pygame and create window pygame.init() pygame.mixer.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_captain("My Game") clock = pygame.time.Clock() all_sprites = pygame.sprite.Group() player = Player() all_sprites.add(player) #Game Loop running = True While runnig: # keep loop running at the right speed clock.tick(FPS) #Process input (events) for event in pygame.event.get(): # check for closing window if event.type == pygame.QUIT: running = False # Update all_sprites.update() #Draw / render screen.fill(BLACK) all_sprites.draw(screen) # *after* drawing everything, flip the display pygame.display.flip() pygame.quit()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question