Lauren Porter wrote:

> 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:

I'd swap the following lines to move the asteroid before drawing it. That 
way the x and y attributes keep in sync with what the user sees.

>         f.asteroid()
>         f.update()

Remove this:

>        #collision detection 'for' loop
>        distance = sqrt(((aX - X)**2) + ((aY - Y)**2))

You can test for a collision and exit if an asteroid is close to a spaceship 
with

    for s in spaceship:
        for a in asteroid:
            if calculate_distance(s, a) < 62.5:
                print("One of your ships was hit by an asteroid")
                exit()

There are two loops because you want to test every asteroid for every 
spaceship (although you only have one at the moment).

I defined calculate_distance() as

def calculate_distance(a, b):
    dx = a.x - b.x
    dy = a.y - b.y
    return sqrt(dx*dx + dy * dy)

As invoked in the for loops `a` will be bound to a spaceship and `b` to an 
asteroid. The function uses the x and y attributes of these objects, so you 
do not need global variables for coordinates.

> def keyTyped():
>     global justPressed
>     justPressed = True
>     print("typed", key)
>             
> def keyPressed():
>     global justPressed
>     justPressed = True
>     print("pressed", key)


-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to