Mar-10-2019, 07:42 PM
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 :).