Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with collisions.
#1
import pygame, sys, random
from pygame.locals import *
from pygame_functions import *

pygame.init()

clock = pygame.time.Clock()
fps = 30

DisplaySurface_Width = 480
DisplaySurface_Height = 640
DisplaySurface = pygame.display.set_mode((DisplaySurface_Height, DisplaySurface_Width))
pygame.display.set_caption("Boat Game")

boatIMG = pygame.image.load("boatIMG.png")
backgroundIMG = pygame.image.load("water_background.png")
heartIMG = pygame.image.load("heart.png")
enemy_bullet = pygame.image.load("bullet_enemy.png")



def bullet(bX, bY):
    DisplaySurface.blit(enemy_bullet, (bX,bY))


def boat(x, y):
    DisplaySurface.blit(boatIMG,(x,y))

game_Exit = False

def game_loop():

    boatx = 400
    boaty = 400
    bulletX = random.randint(200, 500)
    bulletY = -100
    bullet_Speed = 5

    while not game_Exit:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys, exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                boatx -= 5
            elif event.key == pygame.K_RIGHT:
                boatx += 5
            elif event.key == pygame.K_UP:
                boaty -= 5
            elif event.key == pygame.K_DOWN:
                boaty += 5

            if boatx == 120:
                boatx = 125
            elif boatx == 460:
                boatx = 455
            elif boaty == 300:
                boaty = 305
            elif boaty == 410:
                boaty = 405

        DisplaySurface.blit(backgroundIMG, (0, 0))
        boat(boatx, boaty)
        bullet(bulletX, bulletY)
        hit = pygame.sprite.spritecollide(boatIMG, enemy_bullet, False)

        if hit:
            pass #Game over

        if bulletY == 480:
            bulletX = random.randint(200, 500)
            bulletY = -100


        bulletY += bullet_Speed
        pygame.display.update()
        clock.tick(fps)

game_loop()
pygame.quit()
quit()
Error:
AttributeError: 'pygame.Surface' object has no attribute 'rect'
I'm trying to end the game once the boat collides with the bullet. I've tried many tutorials on how to do this, but I just can't seem to figure it out. Any tips or tricks are appreciated :).
Reply


Messages In This Thread
Help with collisions. - by ghost0fkarma - Mar-10-2019, 07:42 PM
RE: Help with collisions. - by Windspar - Mar-10-2019, 08:08 PM
RE: Help with collisions. - by ghost0fkarma - Mar-10-2019, 08:11 PM
RE: Help with collisions. - by ghost0fkarma - Mar-10-2019, 10:26 PM
RE: Help with collisions. - by metulburr - Mar-10-2019, 10:50 PM
RE: Help with collisions. - by Windspar - Mar-11-2019, 12:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Brick [PyGame] How to make collisions in isometrick game? Grigory 1 2,361 Jul-01-2021, 01:54 PM
Last Post: Windspar
  Collisions in Arrays Prithak 2 3,138 Mar-19-2021, 03:17 AM
Last Post: Prithak
  Can someone help me with collisions ? CUKEMAN 2 2,051 Jun-22-2020, 09:54 AM
Last Post: JudyLarose

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020