Running while loop in background
I'm pretty new to Pygame and am trying to make some type of space invaders
game, this is my code so far.
import pygame, sys, os, math, random, time
from pygame.locals import *
pygame.init()
window = pygame.display.set_mode((1000,500))
screen = pygame.display.get_surface()
spaceBackground =
pygame.image.load("C:/Users/LN/Desktop/space-background.png")
spaceShip = pygame.image.load("C:/Users/LN/Desktop/space-ship.png")
bullet = pygame.image.load("C:/Users/LN/Desktop/bullet.png")
class move():
'''Move the space ship'''
def _init_(self):
screen.blit(spaceBackground, (0,0))
self.position = spaceShip.get_rect()
self.position = self.position.move(500,477)
screen.blit(spaceShip,self.position)
pygame.display.flip()
def moveUp(self):
screen.blit(spaceBackground, self.position, self.position)
self.position = self.position.move(0,-1)
global place
place = self.position
screen.blit(spaceShip, self.position)
pygame.time.delay(1)
def moveDown(self):
screen.blit(spaceBackground, self.position, self.position)
self.position = self.position.move(0,1)
global place
place = self.position
screen.blit(spaceShip, self.position)
pygame.time.delay(1)
def moveLeft(self):
screen.blit(spaceBackground, self.position, self.position)
self.position = self.position.move(-1,0)
global place
place = self.position
screen.blit(spaceShip, self.position)
pygame.time.delay(1)
def moveRight(self):
screen.blit(spaceBackground, self.position, self.position)
self.position = self.position.move(1,0)
global place
place = self.position
screen.blit(spaceShip, self.position)
pygame.time.delay(1)
def notTooLow(self):
if self.position[1] < (478):
return True
else:
return False
def notTooHigh(self):
if self.position[1] > (4):
return True
else:
return False
def notTooRight(self):
if self.position[0] < (974):
return True
else:
return False
def notTooLeft(self):
if self.position[0] > (5):
return True
else:
return False
class shoot():
'''Shoots bullets out of the ship'''
def moveUp(self):
self.position = place
self.position = self.position.move(11,0)
while self.position[1] > -4:
screen.blit(spaceBackground, self.position)
self.position = self.position.move(0,-1)
screen.blit(bullet, self.position)
screen.blit(spaceShip, place)
pygame.display.update()
pygame.time.delay(1)
move = move()
move._init_()
shoot = shoot()
while True:
if pygame.key.get_pressed()[K_UP] and move.notTooHigh():
move.moveUp()
pygame.event.pump()
if pygame.key.get_pressed()[K_DOWN] and move.notTooLow():
move.moveDown()
pygame.event.pump()
if pygame.key.get_pressed()[K_ESCAPE]:
sys.exit(0)
pygame.event.pump()
if pygame.key.get_pressed()[K_LEFT] and move.notTooLeft():
move.moveLeft()
pygame.event.pump()
if pygame.key.get_pressed()[K_RIGHT] and move.notTooRight():
move.moveRight()
if pygame.key.get_pressed()[K_SPACE]:
shoot.moveUp()
pygame.display.update()
pygame.event.pump()
So far, when I shoot a bullet, the spaceship stops moving. How could I get
the bullet to move in the background while the rest of the ship keeps
going? This is python 3.3.2.
No comments:
Post a Comment