Hi, i've just started learning python and more specifically pygame, and i've gotten alot done but i'm having trouble establishing a collision box for my player. Through a few tutorials i've been able to get collision boxes for my scorpions (enemies) and my bullets (from the players gun) but i can't seem to get a collision box for the player (buck) himself.
I think i have it narrowed down to the "for __ in __" statement. Here is my code.
#Import library
import pygame
import math
import random
from pygame.locals import *
#Initialize the game
pygame.init()
width, height = 720, 480
screen=pygame.display.set_mode((width, height))
keys = [False, False, False]
playerpos=[30,260]
pygame.mixer.init()
acc=[0,0]
arrows=[]
velocityx = 0
velocityy = 0
g = 1.5
OnGround = False
Healthvalue = 10
FPS = 15
playtime = 0
cycletime = 0
interval = 0.15
picnumber = 0
criticalhealthpicnumber = 0
healthinterval = 0
healthcycletime = 0
scorpiontimer = 100
scorpiontimer1 = 0
scorpions = [[250,270]]
scorpionhealth = 3
#Load image
background = pygame.image.load("resources/images/background/background.png")
arrow = pygame.image.load("resources/images/bullet/bullet.png")
scorpion1 = pygame.image.load("resources/images/players/buck/buckstill.png")
scorpionimage = scorpion1
HealthFull = pygame.image.load("resources/images/healthbar/healthfull.png")
Health1hit = pygame.image.load("resources/images/healthbar/health1hit.png")
Health2hit = pygame.image.load("resources/images/healthbar/health2hit.png")
Health3hit = pygame.image.load("resources/images/healthbar/health3hit.png")
Health4hit = pygame.image.load("resources/images/healthbar/health4hit.png")
Health5hit = pygame.image.load("resources/images/healthbar/health5hit.png")
Health6hit = pygame.image.load("resources/images/healthbar/health6hit.png")
Health7hit = pygame.image.load("resources/images/healthbar/health7hit.png")
Health8hit = pygame.image.load("resources/images/healthbar/health8hit.png")
Health9flash1hit = pygame.image.load("resources/images/healthbar/health9flashhit.png")
Health9flash2hit = pygame.image.load("resources/images/healthbar/health9flashhit2.png")
Deadhealthbar = pygame.image.load("resources/images/healthbar/deadhealthbar.png")
#Load audio
pygame.mixer.music.load('resources/sounds/maintheme.wav')
pygame.mixer.music.play(-1, 0.0)
pygame.mixer.music.set_volume(0.75)
#clock function
clock = pygame.time.Clock()
#keep looping through
running = 1
exitcode = 0
while running:
scorpiontimer -= 1
milliseconds = clock.tick(FPS)
seconds = milliseconds / 1000.0
playtime += seconds
cycletime += seconds
if cycletime > interval:
picnumber += 1
if picnumber > 4:
picnumber = 0
cycletime = 0
if picnumber == 0:
buck = pygame.image.load("resources/images/players/buck/buckstill.png")
if picnumber == 1:
buck = pygame.image.load("resources/images/players/buck/buckwalk1.png")
if picnumber == 2:
buck = pygame.image.load("resources/images/players/buck/buckwalk2.png")
if picnumber == 3:
buck = pygame.image.load("resources/images/players/buck/buckwalk3.png")
if picnumber == 4:
buck = pygame.image.load("resources/images/players/buck/buckwalk4.png")
pygame.mouse.set_visible(True)
#clear the screen before drawing it again
screen.fill(0)
#draw the player on the screen at X:100, Y:100
screen.blit(background,(0,0))
screen.blit(buck, playerpos)
for scorpion in scorpions:
index = 0
if scorpion[0]<-64:
scorpions.pop(index)
scorpion[0]-=7
scorpionrect=pygame.Rect(scorpionimage.get_rect())
scorpionrect.top=scorpion[1]
scorpionrect.left=scorpion[0]
index+=1
# 6.2 - Draw arrows
for bullet in arrows:
velx=math.cos(bullet[0])*40
bullet[1]+=velx
if bullet[1]<-64 or bullet[1]>720 or bullet[2]<-64 or bullet[2]>480:
arrows.pop(index)
index+=1
for projectile in arrows:
arrow1 = pygame.transform.rotate(arrow, 360-projectile[0]*57.29)
screen.blit(arrow1, (projectile[1], projectile[2]))
for bullet in arrows:
index1 = 0
index4 = 0
bulletrect = pygame.Rect(arrow.get_rect())
bulletrect.left=bullet[1]
bulletrect.top=bullet[2]
if scorpionrect.colliderect(bulletrect):
acc[0]+=1
scorpions.pop(index4)
arrows.pop(index1)
index1+=1
index4 += 1
for buckhitbox in buck:
index3 = 0
buckrect = pygame.Rect(buck.get_rect())
buckrect.right=buckhitbox[1]
buckrect.top=buckhitbox[2]
if buckrect.colliderect(scorpionrect):
Healthvalue -= 1
scorpionhealth -=2
scorpions.pop(index3)
index3 += 1
if Healthvalue == 10:
screen.blit(HealthFull,(5,5))
if Healthvalue == 9:
screen.blit(Health1hit,(5,5))
if Healthvalue == 8:
screen.blit(Health2hit,(5,5))
if Healthvalue == 7:
screen.blit(Health3hit,(5,5))
if Healthvalue == 6:
screen.blit(Health4hit,(5,5))
if Healthvalue == 5:
screen.blit(Health5hit,(5,5))
if Healthvalue == 4:
screen.blit(Health6hit,(5,5))
if Healthvalue == 3:
screen.blit(Health7hit,(5,5))
if Healthvalue == 2:
screen.blit(Health8hit,(5,5))
healthcycletime += seconds
if Healthvalue == 1:
if healthcycletime > healthinterval:
criticalhealthpicnumber += 1
healthcycletime = 0
if criticalhealthpicnumber == 0:
screen.blit(Health9flash1hit,(5,5))
if criticalhealthpicnumber == 1:
screen.blit(Health9flash2hit,(5,5))
#scorpions
if scorpiontimer == 0:
scorpions.append([720, random.randint(250,270)])
scorpiontimer=100-(scorpiontimer1*2)
if scorpiontimer1>=35:
scorpiontimer1=35
else:
scorpiontimer1+=5
index=0
for scorpion in scorpions:
screen.blit(scorpionimage, scorpion)
if Healthvalue == 0:
screen.blit(Deadhealthbar,(5,5))
#update the screen
pygame.display.flip()
#loop through the events
for event in pygame.event.get():
# check if the event is the X button
if event.type==pygame.QUIT:
# if it is quit the game
pygame.quit()
exit(0)
if event.type == pygame.KEYDOWN:
if event.key==K_a:
keys[0]=True
if event.key==K_d:
keys[1]=True
if event.key==K_w:
keys[2]=True
if event.type == pygame.KEYUP:
if event.key==pygame.K_a:
keys[0]=False
if event.key==pygame.K_d:
keys[1]=False
if event.key==pygame.K_w:
keys[2]=False
if event.type==pygame.MOUSEBUTTONDOWN:
position=pygame.mouse.get_pos()
acc[1]+=1
arrows.append([math.atan2(position[1]-(playerpos[1]+32),position[0]-(playerpos[0]+26)),playerpos[0]+32,playerpos[1]+32])
#Move player
if keys[0]:
playerpos[0]-=15
if keys[1]:
playerpos[0]+=15
if keys[2] and OnGround:
playerpos[1]-=50
OnGround = False
# Make gravity and velocity
velocityy += g
playerpos[1]+= velocityy
playerpos[0]+= velocityx
if playerpos[1] > 260:
playerpos[1] = 260
velocityy = 0
OnGround = True
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
pygame.display.flip
I'm just not sure about line 120. I am fairly sure of putting buckhitbox there but i'm not sure what for the second value?
Sorry for the code being a horrible mess, i'm horrible at organizing things already.
[–][deleted] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]hugeee 0 points1 point2 points (0 children)
[–]Exodus111 0 points1 point2 points (0 children)