Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import pygame
- from pygame.locals import *
- FPS = 30
- WINWIDTH = 600
- WINHEIGHT = 400
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- GREEN = (0, 255, 0)
- BLUE = (0, 0, 255)
- BLACK = (0, 0, 0)
- GRAY = (120, 127, 121)
- HELL_GRAY = (180, 191, 181)
- class NPC:
- def __init__(self, x, y, dx, dy):
- self.x = x
- self.y = y
- self.dx = dx
- self.dy = dy
- self.bounces = 0
- self.rect = pygame.Rect(0, 0, 0, 0)
- def update(self, player):
- self.x += self.dx
- self.y += self.dy
- if self.rect.colliderect(player.rect):
- self.dy = -self.dy
- self.dx = -self.dx
- if self.y > 400 or self.y < 0:
- self.dy = -self.dy
- self.bounces += 1
- if self.x > 600 or self.x < 0:
- self.dx = -self.dx
- self.bounces += 1
- if self.bounces > 10:
- self.bounces = 0
- def draw(self, surface):
- self.rect = pygame.draw.rect(surface, (255, 0, 0), (self.x, self.y, 20, 20))
- class Paddle:
- def __init__(self, x, y, speed, window):
- self.x = x
- self.y = y
- self.speed = speed
- self.window = window
- self.right = False
- self.left = False
- self.points = 0
- self.rect = pygame.Rect(0, 0, 0, 0)
- def draw(self, surface):
- self.rect = pygame.draw.rect(surface, (0, 0, 255), (self.x, self.y, 100, 20))
- def update(self):
- if self.right and self.x < self.window[0] - 100:
- self.x += self.speed
- elif self.left and self.x > 0:
- self.x -= self.speed
- def main():
- pygame.init()
- FPSCLOCK = pygame.time.Clock()
- DISPLAYSURF = pygame.display.set_mode((WINWIDTH, WINHEIGHT))
- pygame.display.set_caption('Juego 1')
- BFONT = pygame.font.Font('freesansbold.ttf', 24)
- SFONT = pygame.font.Font('freesansbold.ttf', 12)
- npcs = [NPC(0, 0, 5, 10), NPC(20, 0, 5, 10), NPC(0, 20, 1, 10), NPC(190, 0, 8, 2)]
- player = Paddle(300, 380, 15, (WINWIDTH, WINHEIGHT))
- while True:
- DISPLAYSURF.fill(WHITE)
- player.update()
- player.draw(DISPLAYSURF)
- for npc in npcs:
- npc.draw(DISPLAYSURF)
- npc.update(player)
- pygame.draw.circle(DISPLAYSURF, (0, 0, 255), (50, 100), 50)
- for event in pygame.event.get(): # event handling loop
- if event.type == QUIT:
- terminate()
- elif event.type == KEYDOWN:
- if event.key == K_RIGHT:
- player.right = True
- elif event.key == K_LEFT:
- player.left = True
- elif event.type == KEYUP:
- if event.key == K_RIGHT:
- player.right = False
- elif event.key == K_LEFT:
- player.left = False
- pygame.display.update()
- FPSCLOCK.tick(FPS)
- def terminate():
- pygame.quit()
- sys.exit()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement