Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- pygame.init()
- WIDTH = 800
- HEIGHT = 600
- SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- class player_class:
- def __init__(self, x, y, size, color):
- self.rect = pygame.Rect(x, y, size, size)
- self.color = color
- self.changeX = 0
- self.changeY = 0
- def update(self):
- self.rect.x += self.changeX
- self.rect.y += self.changeY
- # Friction
- self.changeY = self.changeY * 0.85
- self.changeX = self.changeX * 0.85
- def draw(self, SCREEN):
- pygame.draw.rect(SCREEN, self.color, self.rect)
- def up(self):
- self.changeY -= 1
- def down(self):
- self.changeY += 1
- def right(self):
- self.changeX += 1
- def left(self):
- self.changeX -= 1
- player = player_class(400, HEIGHT - 100, 25, WHITE)
- clock = pygame.time.Clock()
- playing = True
- while playing:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- playing = False
- # Handle Keyboard inputs
- keys = pygame.key.get_pressed()
- if keys[pygame.K_LEFT]:
- player.left()
- if keys[pygame.K_RIGHT]:
- player.right()
- if keys[pygame.K_UP]:
- player.up()
- if keys[pygame.K_DOWN]:
- player.down()
- # Update sprites
- player.update()
- SCREEN.fill(BLACK)
- player.draw(SCREEN)
- pygame.display.update()
- clock.tick(60)
- pygame.quit()
- # We now have a functioning player
- # Make an enemy class and make enemies spawn
- # from the top of the screen with random
- # speed and size
- # Challenge: make the player die when they touch
- # an enemy
Advertisement
Add Comment
Please, Sign In to add comment