Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Pygame template - skeleton for a new pygame project
- import pygame
- import random
- WIDTH = 800
- HEIGHT = 800
- FPS = 30
- TIMER = 0
- Xstep = 8
- Ystep = 7
- # define colors
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- RED = (255, 0, 0)
- GREEN = (0, 255, 0)
- BLUE = (0, 0, 255)
- def RandomColor():
- tuple01 = (WHITE,RED,GREEN,BLUE)
- return(tuple01[random.randint(0,3)])
- # initialize pygame and create window
- pygame.init()
- pygame.mixer.init()
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption("My Game")
- clock = pygame.time.Clock()
- all_sprites = pygame.sprite.Group()
- class Player(pygame.sprite.Sprite):
- def __init__(self):
- pygame.sprite.Sprite.__init__(self)
- self.image = pygame.Surface((40, 40))
- self.image.fill(GREEN)
- self.rect = self.image.get_rect()
- self.rect.center = (WIDTH / 2, HEIGHT / 2)
- def update(self):
- global Xstep
- global Ystep
- global TIMER
- if TIMER == 20:
- TIMER = 0
- else:
- TIMER += 1
- #movements
- self.rect.x += Xstep
- self.rect.y += Ystep
- if self.rect.right > WIDTH:
- Xstep =- Xstep
- if self.rect.left <= 0:
- Xstep =- Xstep
- if self.rect.top <= 0:
- Ystep =- Ystep
- if self.rect.bottom > HEIGHT:
- Ystep =- Ystep
- if TIMER == 10:
- self.image.fill(RandomColor())
- print(TIMER)
- greensqr = Player()
- all_sprites.add(greensqr)
- # Game loop
- running = True
- while running:
- # 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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement