1 changed files with 85 additions and 0 deletions
@ -0,0 +1,85 @@ |
|||||
|
import pygame, sys |
||||
|
from pygame.locals import * |
||||
|
import random |
||||
|
|
||||
|
pygame.init() |
||||
|
|
||||
|
SCREEN_WIDTH = 400 |
||||
|
FPS = 60 |
||||
|
FramePerSec = pygame.time.Clock() |
||||
|
|
||||
|
BLUE = (0, 0, 255) |
||||
|
RED = (255, 0, 0) |
||||
|
GREEN = (0, 255, 0) |
||||
|
BLACK = (0, 0, 0) |
||||
|
WHITE = (255, 255, 255) |
||||
|
|
||||
|
DISPLAYSURF = pygame.display.set_mode((400,600)) |
||||
|
DISPLAYSURF.fill(WHITE) |
||||
|
pygame.display.set_caption("Game") |
||||
|
|
||||
|
class Enemy(pygame.sprite.Sprite): |
||||
|
def __init__(self): |
||||
|
super().__init__() |
||||
|
self.image = pygame.image.load("Enemy.png") |
||||
|
self.rect = self.image.get_rect() |
||||
|
self.rect.center=(random.randint(40,SCREEN_WIDTH-40),0) |
||||
|
|
||||
|
def move(self, player): |
||||
|
self.rect.move_ip(0,10) |
||||
|
if (self.rect.bottom > 600): |
||||
|
self.rect.top = 0 |
||||
|
self.rect.center = (random.randint(30, 370), 0) |
||||
|
|
||||
|
if self.rect.colliderect(player.rect): |
||||
|
print("HIT") |
||||
|
|
||||
|
def draw(self, surface): |
||||
|
surface.blit(self.image, self.rect) |
||||
|
|
||||
|
|
||||
|
class Player(pygame.sprite.Sprite): |
||||
|
def __init__(self): |
||||
|
super().__init__() |
||||
|
self.image = pygame.image.load("Player.png") |
||||
|
self.rect = self.image.get_rect() |
||||
|
self.rect.center = (160, 520) |
||||
|
|
||||
|
def update(self): |
||||
|
pressed_keys = pygame.key.get_pressed() |
||||
|
if pressed_keys[K_UP]: |
||||
|
self.rect.move_ip(0, -5) |
||||
|
if pressed_keys[K_DOWN]: |
||||
|
self.rect.move_ip(0,5) |
||||
|
|
||||
|
if self.rect.left > 0: |
||||
|
if pressed_keys[K_LEFT]: |
||||
|
self.rect.move_ip(-5, 0) |
||||
|
if self.rect.right < SCREEN_WIDTH: |
||||
|
if pressed_keys[K_RIGHT]: |
||||
|
self.rect.move_ip(5, 0) |
||||
|
|
||||
|
def draw(self, surface): |
||||
|
surface.blit(self.image, self.rect) |
||||
|
|
||||
|
def get_pos(self): |
||||
|
return self.rect.pos |
||||
|
|
||||
|
|
||||
|
P1 = Player() |
||||
|
E1 = Enemy() |
||||
|
|
||||
|
while True: |
||||
|
for event in pygame.event.get(): |
||||
|
if event.type == QUIT: |
||||
|
pygame.quit() |
||||
|
sys.exit() |
||||
|
P1.update() |
||||
|
E1.move(P1) |
||||
|
|
||||
|
DISPLAYSURF.fill(WHITE) |
||||
|
P1.draw(DISPLAYSURF) |
||||
|
E1.draw(DISPLAYSURF) |
||||
|
|
||||
|
pygame.display.update() |
||||
|
FramePerSec.tick(FPS) |
Loading…
Reference in new issue