Python Forum
[PyGame] Help making enemys?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Help making enemys?
#1
import pygame, sys
from pygame.locals import *

pygame.init()

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

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

white = (255, 255, 255)
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")

boatx = 400
boaty = 400

while True:
    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])
    DisplaySurface.blit(boatIMG, (boatx, boaty))




    pygame.display.update()
    fpsClock.tick(fps)
Hello, I would like to make a game where objects(enemy_bullet) come down from the top of the screen and the player(boatIMG) has to dodge them. I'm very confused on how to go about this. Any tips and tricks are appreciated!
Reply
#2
get a random location on the x axis, and create a bullet object that increases Y value to drop down.
Recommended Tutorials:
Reply
#3
(Mar-07-2019, 05:12 PM)metulburr Wrote: get a random location on the x axis, and create a bullet object that increases Y value to drop down.

Thank you for the response and help! I have finally managed to get it working. But, I face another problem. How could I exit the game once the bullet() hits the boat()?

import pygame, sys, random
from pygame.locals 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")

boatx = 400
boaty = 400

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





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





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










game_Exit = False

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)

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

    bulletY += bullet_Speed












    pygame.display.update()
    clock.tick(fps)




pygame.quit()
quit()
Reply
#4
create a pygame.Rect for the boat, and one for the bullet. If the two collide, then it hit the boat.
Our tutorial for that here
https://python-forum.io/Thread-PyGame-En...ion-part-6
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

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