1

Basically I'm trying to have it so that when the ship touches the meteor it plays an explode animation. I'm using colour detection for this as when i used player.colliderect(name) it was really inaccurate and began the animation when it was far away from the asteroid. So im using colour detection now in hopes that it'll work better and be more accurate. This is the code i tried making for that (i know its commented out):

# pixel_colour = screen.get_at(player.pos) #collision with colour makes ship explode
#     red = pixel_colour[0]
#     blue = pixel_colour[1]
#     green = pixel_colour[2]
#     if red and green and blue != 0:
#         print("oof")
#         gameover() 

Here is the gameover() subroutine:

def gameover(): #ship exploding animation
    player.image = 'explode.png'
    clock.schedule_unique(game_over, 0.3)
def game_over():
    player.image = 'explode1.png'
    clock.schedule_unique(overgame, 0.3)
def overgame():
    player.image = 'explode2.png'
    vel = 0 #stop the ship from moving (doesnt work)

it says "screen" doesn't have the attribute "get_at" so i tried it with "surface" instead of "screen" and it said that "surface is not defined" I really dont know what to do with it... Also i tried to make the player stop being able to move when they get a game over (as seen inside the second bit of code when i make vel 0 it says "vel is not defined" and "vel is used before assignment") Heres my full code so you know what im doing :D

import pgzrun
import pygame
import random




WIDTH = 850
HEIGHT = 425

#sprites

player = Actor('ship.png')
player.pos = 100, 56
foe = Actor('foe.png')
foe.pos = 200, 112
foe2 = Actor('foe.png')
foe2.pos = 200, 112
title = Actor('title2.png')      #calling sprites and saying their pos
title.pos = 400, 212
cont = Actor('cont.png')
cont.pos = 470, 300
vel = 5


music.play("temp.mp3")

#controls

def draw():
    screen.clear()
    screen.fill((0, 0, 0))
    foe.draw()
    foe2.draw()
    player.draw()
    title.draw()
    cont.draw()


def update():
    keys = pygame.key.get_pressed()
    x, y = player.pos #controls
    x += (keys[pygame.K_d] - keys[pygame.K_a]) * vel
    y += (keys[pygame.K_s] - keys[pygame.K_w]) * vel
    player.pos = x, y
    
    if player.left > WIDTH: #if player hits right side of screen it takes the player to the other side of the screen
        player.right = 0
        
        
#     pixel_colour = screen.get_at(player.pos) #collision with colour makes ship explode
#     red = pixel_colour[0]
#     blue = pixel_colour[1]
#     green = pixel_colour[2]
#     if red and green and blue != 0:
#         print("oof")
#         gameover()
         
def foeupdate():
    moveamount = 0
    while moveamount <= 5:
        fx, fy = foe.pos #making asteroid appear in random places.
        fx = random.randint(0, 850)
        fy = random.randint(0, 425)
        duration = random.randint(0, 5)
        animate(foe, pos = (fx, fy), duration)
        moveamount =+ 1
    #foe.pos = fx, fy

def otherimage():           #animation of ship
    player.image = 'ship.png'
    clock.schedule_unique(imagey, 0.3)
def imagey():
    
    player.image = 'ship1.png'
    clock.schedule_unique(otherimage, 0.3)
    
    


def gameover(): #ship exploding animation
    player.image = 'explode.png'
    clock.schedule_unique(game_over, 0.3)
def game_over():
    player.image = 'explode1.png'
    clock.schedule_unique(overgame, 0.3)
def overgame():
    player.image = 'explode2.png'
    vel = 0 #stop the ship from moving (doesnt work)

imagey()
foeupdate()

pgzrun.go()

also i have experimented with wrapping (as you can see on the line that goes if player.left > WIDTH: player.right = 0) i would like this to be the same with the other sides as well. But im trying to work this out at the moment so if you don't talk about this then its fine because im sure i can figure it out haha :D

so the code is working perfectly fine but i would like to get these things working as its the core of the game. Thanks if you can help :D

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Whi_24
  • 41
  • 4

1 Answers1

1

You asked several questions.

I'm using colour detection for this as when i used player.colliderect(name) it was really inaccurate and began the animation when it was far away from the asteroid.

No, this is the wrong choice. You have to use "mask" collision. See PyGame collision with masks or Collision between masks in PyGame or Check collision between a image and a line.


i tried to make the player stop being able to move when they get a game over (as seen inside the second bit of code when i make vel 0 it says "vel is not defined" and "vel is used before assignment")

You must use the global statement if you want to change a variable in global namespace within a function:

vel = 0
def overgame():
    global vel

    # [...]

    vel = 0
Rabbid76
  • 202,892
  • 27
  • 131
  • 174