Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- pygame.init()
- #Constants
- #Colors
- BLACK = (0,0,0)
- RED = (255, 0, 0)
- WHITE = (0,0,0)
- screen = pygame.display.set_mode((700,500))
- #variables
- clock = pygame.time.Clock()
- running = True
- count = 0
- #classes
- class Block:
- def __init__(self, color, x, y, width, height):
- self.color = color
- self.x = x
- self.y = y
- self.width = width
- self.height = height
- def display(self):
- pygame.draw.rect(screen, self.color, [self.x,self.y,self.width,self.height])
- def move(self,direction):
- if direction == "LEFT":
- self.x = self.x - 1
- elif direction == "RIGHT":
- self.x = self.x + 1
- elif direction == "DOWN":
- self.y = self.y + 1
- elif direction == "UP":
- self.y = self.y - 1
- def UpdateDisplay():
- screen.fill(WHITE)
- A.display()
- pygame.display.flip()
- A = Block(RED, 10, 10, 20, 20)
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- elif event.type == pygame.KEYDOWN:
- print(event.key)
- keys = pygame.key.get_pressed()
- if keys[pygame.K_a]:
- A.move("LEFT")
- elif keys[pygame.K_d]:
- A.move("RIGHT")
- elif keys[pygame.K_w]:
- A.move("UP")
- elif keys[pygame.K_s]:
- A.move("DOWN")
- clock.tick(60)
- UpdateDisplay()
- pygame.QUIT()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement