Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, time
- from pygame.locals import *
- from pygame.color import THECOLORS
- pygame.init()
- # Main Variables
- size = width, height = (640, 480)
- screen = pygame.display.set_mode(size)
- face = pygame.Surface((100, 100))
- faceRect = face.get_rect()
- backColor = THECOLORS['white']
- speed = [2, 2]
- # Program Stuff
- screen.fill(backColor)
- face.fill(backColor)
- pygame.display.set_caption("Ex 38")
- clock = pygame.time.Clock()
- # Draw Happy Face
- pygame.draw.circle(face, THECOLORS['black'], (50, 50), 50)
- pygame.draw.circle(face, THECOLORS['white'], (50, 64), 25)
- pygame.draw.rect(face, THECOLORS['black'], (20, 30, 60, 36))
- pygame.draw.line(face, THECOLORS['white'], (35, 20), (35, 40), 4)
- pygame.draw.line(face, THECOLORS['white'], (65, 20), (65, 40), 4)
- # Game Loop
- pygame.display.flip()
- keepGoing = True
- while keepGoing:
- clock.tick(240)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- keepGoing = False
- # Get mouse data
- mouse = pygame.mouse.get_pressed()
- pos = pygame.mouse.get_pos()
- # Mouse Position Setting
- if mouse[0]:
- faceRect.centerx = pos[0]
- faceRect.centery = pos[1]
- # Automated Movement and Error correction
- faceRect = faceRect.move(speed)
- if faceRect.left < 0:
- faceRect.left = 0
- speed[0] *= -1
- elif faceRect.right > width:
- faceRect.right = width
- speed[0] *= -1
- if faceRect.top < 0:
- faceRect.top = 0
- speed[1] *= -1
- elif faceRect.bottom > height:
- faceRect.bottom = height
- speed[1] *= -1
- # Re-draw
- screen.fill(backColor)
- screen.blit(face, faceRect)
- pygame.display.flip()
- pygame.quit()
Add Comment
Please, Sign In to add comment