Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- import random
- class TheGhost:
- def __init__(self, image, location):
- self.the_image = pygame.image.load(image)
- self.the_rect = self.the_image.get_rect()
- self.the_rect.left = location[0]
- self.the_rect.top = location[1]
- self.speed = [random.randint(-2, 2), random.randint(-2, 2)]
- def move(self):
- self.the_rect = self.the_rect.move(self.speed)
- def check_edge(self, width, height):
- if self.the_rect.left < 0 or self.the_rect.right > width:
- self.speed[0] = -self.speed[0]
- if self.the_rect.top < 0 or self.the_rect.bottom > height:
- self.speed[1] = -self.speed[1]
- def blit(self, the_surface):
- the_surface.blit(self.the_image, self.the_rect)
- class MySprite:
- def __init__(self, image, location):
- self.the_image = pygame.image.load(image)
- self.the_rect = self.the_image.get_rect()
- self.the_rect.left = location[0]
- self.the_rect.top = location[1]
- def move_left(self):
- check = self.the_rect.left - 5
- if check > 0:
- self.the_rect.left = check
- def move_right(self, edge):
- check = self.the_rect.right + 5
- if check < edge:
- self.the_rect.right = check
- def move_up(self):
- check = self.the_rect.top - 5
- if check > 0:
- self.the_rect.top = check
- def move_down(self, edge):
- check = self.the_rect.bottom + 5
- if check < edge:
- self.the_rect.bottom = check
- def blit(self, the_surface):
- the_surface.blit(self.the_image, self.the_rect)
- class MainWindow:
- def __init__(self):
- pygame.init()
- pygame.key.set_repeat(10, 10)
- self.width = 1200
- self.height = 700
- size = (self.width, self.height)
- self.DISPLAYSURF = pygame.display.set_mode(size)
- pygame.display.set_caption("FireTech")
- self.player = MySprite("player.png", (300, 300))
- self.ghost = TheGhost("ghost.png", (100, 100))
- def main_game_loop(self):
- while True: # main game loop
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT:
- self.player.move_left()
- elif event.key == pygame.K_RIGHT:
- self.player.move_right(self.width)
- elif event.key == pygame.K_UP:
- self.player.move_up()
- elif event.key == pygame.K_DOWN:
- self.player.move_down(self.height)
- self.DISPLAYSURF.fill((0, 0, 0))
- self.player.blit(self.DISPLAYSURF)
- self.ghost.move()
- self.ghost.check_edge(self.width, self.height)
- self.ghost.blit(self.DISPLAYSURF)
- pygame.display.update()
- pygame.display.flip()
- my_game = MainWindow()
- my_game.main_game_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement