Processing Game Help

Lauren Porter lnporter99 at gmail.com
Sun Dec 10 19:05:37 EST 2017


Hello all! I've been trying to create a game in Python Processing where a spaceship moves horizontally in order to miss a collision with an asteroid. I'm having difficulty making it so that the game quits when an asteroid hits the spaceship, could anybody help? Here is my code. As you can see, I tried using globals in order use variables from previous classes, but nothing has worked. For this specific approach, no error message popped up, but nothing happened either. Thanks so much!

COLORS = {"black":"#000000", "white":"#FFFFFF",
          "red":"#FF0000", "green":"#00FF00",
          "blue":"#D1F5FF", "yellow":"#FFFF00",
          "orange":"#FFA500", "hendrixorange":"#F58025",
          "purple":"#9B30FF", "gray":"#808080", "lightgray": "#CACACA",
          "darkgray":"#A9A9A9"}
import random
asteroid = []
spaceship = []
stars = []
justPressed = False
aX = 0
aY = 0
x = 0
y = 0

def setup():
    global aX, aY, X, Y
    size(640, 480)
    for s in range(1):
        X = 320
        Y = 440
        spaceship.append(Space(X, Y, COLORS["orange"]))
    for a in range(4):
        aX = random.randint(0, 640)
        aY = 0
        asteroid.append(Asteroid(aX, aY, COLORS["orange"]))
    for d in range(100):
        randX = random.randint(0, 640)
        randY = random.randint(0, 480)
        stars.append(Star(randX, randY, COLORS["orange"]))



class Space:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color   

    def spaceship(self):
        fill(COLORS["blue"])
        stroke(COLORS["white"])
        ellipse(self.x, self.y, 75, 75) #body
        fill(COLORS["green"])
        fill(COLORS["gray"])
        stroke(COLORS["green"])
        ellipse(self.x, self.y + 20, 120, 35)
        stroke(COLORS["orange"])
        fill(COLORS["purple"])
        ellipse(self.x, self.y + 20, 10, 10)
        ellipse(self.x + 30, self.y + 20, 10, 10)
        ellipse(self.x - 30, self.y + 20, 10, 10)
    
    def move(self, dx, dy):
        self.x += dx
        self.y += dy

class Asteroid:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color
            
    def update(self):
        self.velx = 0
        self.vely = random.randint(1, 5)
        self.x += self.velx
        self.y += self.vely
        if self.y > 480:
            self.y = 0
            self.x = random.randint(1,640)
        
    def asteroid(self):
        fill(COLORS["lightgray"])
        stroke(COLORS["white"])
        ellipse(self.x, self.y, 50, 50)
        fill(COLORS["gray"])
        ellipse(self.x +15, self.y + 6, 15, 15)
        ellipse(self.x -12, self.y + 7, 10, 10)
        ellipse(self.x + 5, self.y - 13, 11, 11)
        ellipse(self.x - 12, self.y - 9, 7, 7)
        ellipse(self.x + 2, self.y + 18, 7, 7)
            
class Star:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color   

    def star(self):
        fill(COLORS["white"])
        ellipse(self.x, self.y, 3, 3)


def draw():
    global justPressed
    background(COLORS["black"])
    for f in stars:
        f.star()
    for f in spaceship:
        f.spaceship()
        if f.x < 60:
            f.move(3, 0)
        elif f.x > 580:
            f.move(-3, 0)
        elif justPressed :
            if key == 'd':
                f.move(3, 0)
            elif key == 'a':
                f.move(-3, 0)
    for f in asteroid:
        f.asteroid() 
        f.update()
        #collision detection 'for' loop
        distance = sqrt(((aX - X)**2) + ((aY - Y)**2))
        for f in asteroid:
            if distance < 62.5:
                background(COLORS["red"])
        
            
def keyTyped():
    global justPressed
    justPressed = True
    print("typed", key)
            
def keyPressed():
    global justPressed
    justPressed = True
    print("pressed", key)




More information about the Python-list mailing list