Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import pygame
- import sys
- import time
- #Setting up pygame
- pygame.init()
- shooting = False
- goingright = 1
- keys = [False,False,False,False]
- clock = pygame.time.Clock()
- screen = pygame.display.set_mode([500,500])
- font = pygame.font.Font(None,50)
- #Creating class for player
- class Player:
- def __init__(self,x,y,width,height):
- self.x = x
- self.y = y
- self.width = width
- self.height = height
- def draw(self):
- pygame.draw.rect(screen,[0,255,0],[int(self.x),int(self.y),int(self.width),int(self.height)],0)
- def move(self):
- if keys[1] == True:
- self.x -= 1
- elif keys[3] == True:
- self.x += 1
- if self.x < 0:
- print(self.x)
- self.x = 0
- if self.x > 500 - self.width:
- print(self.x)
- self.x = 500 - self.width
- def shoot(self):
- return
- class Bullet():
- def __init__(self,x,y):
- self.x = x
- self.y = y
- def update(self,y_amount = 5):
- self.y -= y_amount
- return
- def draw(self):
- self.rect = pygame.draw.rect(screen,[0,255,0],[int(self.x),int(self.y),10,50],0)
- class Alien():
- def __init__(self,x,y,goingright,dead):
- self.x = x
- self.y = y
- self.goingright = goingright
- self.dead = dead
- def draw(self):
- self.rect = pygame.draw.rect(screen,[0,255,0],[int(self.x),int(self.y),25,25],0)
- def move(self):
- if self.goingright == True:
- self.x += 0.1
- if self.x > 500 - 25:
- self.y+=25
- self.x = 475
- self.goingright = False
- elif self.goingright == False:
- self.x -= 0.1
- if self.x < 0:
- self.y+=25
- self.x = 0
- self.goingright = True
- bullets = []
- aliens = []
- #Creating a player
- player = Player(200,450,40,20)
- for i in range(100,800,75):
- aliens.append(Alien(i,100,True,False))
- #Main Loop
- while True:
- #Background
- screen.fill([0,0,0])
- #Letting Player move
- player.move()
- #Drawing Player
- player.draw()
- #Updating screen
- #Checking for events
- for event in pygame.event.get():
- #Checking for quit
- if event.type == pygame.QUIT:
- sys.exit()
- if event.type == pygame.KEYDOWN:
- #Checking for keys
- if event.key == pygame.K_w:
- keys[0] = True
- elif event.key == pygame.K_a:
- keys[1]=True
- elif event.key == pygame.K_s:
- keys[2]=True
- elif event.key == pygame.K_d:
- keys[3]=True
- elif event.key == pygame.K_SPACE:
- bullets.append(Bullet(player.x, player.y))
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_w:
- keys[0]=False
- elif event.key == pygame.K_a:
- keys[1]=False
- elif event.key == pygame.K_s:
- keys[2]=False
- elif event.key == pygame.K_d:
- keys[3]=False
- elif event.key == pygame.K_SPACE:
- shooting = True
- shooting = False
- c = 0
- for bullet in bullets:
- bullet.draw()
- bullet.update()
- for alien in aliens:
- alien.draw()
- if alien.dead == False:
- alien.move()
- for alien in aliens:
- for bullet in bullets:
- if alien.x < bullet.x + 10 and alien.x + 25 > bullet.x and alien.y < bullet.y + 50 and 25 + alien.y > bullet.y:
- pygame.display.flip()
Add Comment
Please, Sign In to add comment