Reputation: 119
Im a Python noob trying to make a simple Pygame as a school assignment. The player controlls a bat in the x and y axis. The point of the game is to catch the mosquitos flying around, one by one. Mosquito position is random, and it moves around the screen.
My problem is how to detect when the bat catches the mosquito?
def crash():
global mosquito_y
if bat_y < (mosquito_y + mosquito_size):
if ((mosquito_x < bat_x < (mosquito_x + mosquito_size)) or
((bat_x + bat_width) > mosquito_x and (bat_x + bat_width) < (mosquito_x + mosquito_size))):
mosquito_y += 1000
I tried this code but it "catches" the mosquito in the whole y axis, not just the bats y-position.
Any ides how to solve this?
Heres all my code. Sorry for the comments in Norwegian:
import pygame
import random
"""Variablene som definerer vinduet og bakgrunnsbilde"""
sc_width = 800 # variabler for skjermstørrelsen
sc_height = 600
background = pygame.image.load("swamp.jpg")
"""Initialiserer pygame og oppretter spillervinduet samt tittel"""
pygame.init() # starter spillet
screen = pygame.display.set_mode((sc_width, sc_height)) # setter skjermstørrelsen
pygame.display.set_caption("Myggjakten") # setter tittelen på spillet
"""Variablene til flaggermusen i spillet"""
bat_img = pygame.image.load("Bat.png") # laster inn bilde
bat_img = pygame.transform.scale(bat_img, (100, 50)) # skalerer bildet, høyde og bredde
bat_x = 300
bat_y = 470
bat_x_change = 0
bat_y_change = 0
bat_width = 50 # variable som inneholder halvparten av bredden til flaggermusen
bat_height = 25
"""Variablene til myggen i spillet"""
mosquito_img = pygame.image.load("mosquito.png") # laster inn bilde
mosquito_img = pygame.transform.scale(mosquito_img, (50, 25)) # skalerer bildet, høyde og bredde
mosquito_x = random.randint(0, 800)
mosquito_y = random.randint(50, 400)
mosquito_x_change = 0.4
mosquito_y_change = 0.4
mosquito_size = 25 # variabel som inneholder halve bredden til myggen
def crash():
"""This code was a suggestion, but dosent seem to work"""
global mosquito_y
if (mosquito_y + mosquito_size) in range(bat_y + bat_height):
if (bat_x + bat_width) in range(mosquito_x + mosquito_size):
mosquito_y += 1000
"""poengberegning"""
point = 0
"""Loopen som kjører spillet helt til det enten er game over, vinn eller brukeren lukker vinduet"""
running = True
while running:
# evig loop som kjører programmet helt til running till false
for event in pygame.event.get(): # henter listen med eventer fra pygame
if event.type == pygame.QUIT: # ved kommando quit i programmet
running = False # running settes til false og programmet avsluttes
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT: # når brukeren trykker venstre piltast
bat_x_change = -1 # blir x-verdien 0,25 mindre per frame
if event.key == pygame.K_UP: # samme som over, men her blir y-verdien endre på pil opp
bat_y_change = -1 # hastigheten den bevegers seg i y-aksen
if event.key == pygame.K_RIGHT: # samme som over, men for x til høyre
bat_x_change = 1
if event.key == pygame.K_DOWN: # samme som over, men for y nedover
bat_y_change = 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP \
or event.key == pygame.K_DOWN: # når tastene definert over ikke trykkes lenger
bat_x_change = 0 # stopper bevegelsen
bat_y_change = 0
"""setter opp grenser for flaggermusen så den ikke får flydd utenfor skjermen"""
bat_x += bat_x_change # Setter maksverdier for x og y for å hindre at bat kan fly utenfor spillskjermen
if bat_x <= 0:
bat_x = 0
elif bat_x >= 700: # hvis bat_x er større enn 700 eller mindre enn o settes posisjonen til 700 eller o
bat_x = 700
bat_y += bat_y_change
if bat_y <= 0: # hvis y-verdien (opp og ned) blir mindre enn 0 eller større enn 550 settes posisjonen til 0 eller
# 550
bat_y = 0
elif bat_y >= 550:
bat_y = 550
"""Setter grenser for myggen så den beveger seg i ulike retninger, men aldri utenfor skjermen"""
mosquito_x += mosquito_x_change # Setter maksverdier for x og y for å hindre at bat kan fly utenfor spillskjermen
mosquito_y += mosquito_y_change
if mosquito_x <= 0:
mosquito_x_change = 0.4
elif mosquito_x >= 770: # hvis MOSQUITO_x er større enn 500 eller mindre enn o settes posisjonen til 700 eller o
mosquito_x_change = -0.4
elif mosquito_y <= 0:
mosquito_y_change = 0.3
elif mosquito_y >= 570:
mosquito_y_change = -0.3
"""Oppdaterer spillvinduet med de endringer som har skjedd i loopen"""
# Legger på bakgrunnsbilde(Swamp.jpg)
screen.blit(background, (0, 0))
screen.blit(bat_img, (bat_x, bat_y)) # setter posisjonen til bildet på skjermen
screen.blit(mosquito_img, (mosquito_x, mosquito_y)) # setter posisjonen til bildet på skjermen
crash()
pygame.display.update() # oppdaterer pygame-skjermen med den nye fargen og evt andre endringer
Upvotes: 1
Views: 113
Reputation: 119
Thank for all the input. I ended up creating a rect() around both bat and mosquito. From there it was pretty simple, so thanks for the advise.
Heres what I ended up with. It detects collissions, ads point and creates a new mosquito until 20 points is reached and the game ends.
I also added the _change-variable so the rect moves with the image.
while running:
bat_rect = pygame.Rect((bat_x + bat_x_change), (bat_y + bat_y_change), 100, 40)
mosquito_rect = pygame.Rect((mosquito_x + mosquito_x_change), (mosquito_y + mosquito_y_change), 50, 25)
if bat_rect.colliderect(mosquito_rect):
point += 1
print(point)
if point <= 20:
mosquito_x = random.randint(0, 800)
mosquito_y = random.randint(0, 600)
else:
running = False
Upvotes: 1
Reputation: 793
I think you would have to add the range of the bat, something like this
killed = 0
import random
def crash():
global mosquito_y
if (mosquito_y + mosquito_size) in range(bay_y + bat_size): #added the bat size
if ((bat_x+bat_width) in range(mosquito_x + mosquito_size)):
#just changed the bat width and x value
killed = killed + 1 #this will add one to the number of killed mosquitos
print(f'{killed} mosquitos has been smashed')
mosquito_y = random.random(0, 400)
mosquito_x = random.random(0, 400)#these 2 lines will put the mosquiot in random position
#the code to count a new one could be something like this
Upvotes: 1
Reputation:
Try doing it this way
make the bat a rect like this
bat_rect = #the variable name of bat.get_rect(topleft = (x,y)
do the same for the mosquito
the bat moves with (W,A,S,D)
if even.type == pygame.KEYDOWN:
if event.key == pyagme.K_w:
y += 6
if bat_rect.colliderect(mosquito_rect):
mosquito_catched += 1
if event.key == pyagme.K_s:
y -= 6
if bat_rect.colliderect(mosquito_rect):
mosquito_catched += 1
if event.key == pyagme.K_a:
x -= 6
if bat_rect.colliderect(mosquito_rect):
mosquito_catched += 1
if event.key == pyagme.K_d:
z += 6
if bat_rect.colliderect(mosquito_rect):
mosquito_catched += 1
Upvotes: 2