I am making a game in which a ninja has to dodge obstacles coming in front of him, the obstacles are chosen randomly by the computer using random.randint() in the method choose() in obstacle class. The problem is that the choose() method chooses a random number in every iteration of the main loop. I want that if the x position of the obstacle is 0 then only it should choose another random number.
Remember that I am a beginner
Here is my code:
import pygame,time,random #import
pygame.init() #pygame initialization
screen = pygame.display.set_mode((0,0))
pygame.quit()
pygame.init()
clock = pygame.time.Clock()
canvas = pygame.display.set_mode((1000,780)) #creating screen
caption = pygame.display.set_caption("Ninja Run") #creating caption
iconImage = pygame.image.load("C:/Garv/python/pygame/Ninja Game/Ninja_logo.png")
icon = pygame.display.set_icon(iconImage) #drawing icon image
land = pygame.image.load("C:/Garv/python/pygame/Ninja Game/land.jfif") #land image
sky = pygame.image.load("C:/Garv/python/pygame/Ninja Game/sky.jfif") # sky image
x = 50
y = 594
start = time.time()
ninjaRun = [pygame.image.load("C:/Garv/python/pygame/Ninja Game/ninja running 1.jpg"),pygame.image.load("C:/Garv/python/pygame/Ninja Game/ninja running 2.jpg")] #ninja images
ninjaImage = ninjaRun[0]
ninjaState = "Run"
ninjaStars = [pygame.image.load("C:/Garv/python/pygame/Ninja Game/ninja star 2.jfif"),pygame.image.load("C:/Garv/python/pygame/Ninja Game/ninja star.jfif")]
tyre = pygame.image.load("C:/Garv/python/pygame/Ninja Game/tyre.jfif")
rock = pygame.image.load("C:/Garv/python/pygame/Ninja Game/rock.jfif")
rockX = 1000
rockY = 605
starX1 = 1050
starY1 = 580
starX2 = 1000
starY2 = 650
tyreX = 550
tyreY = 500
class obstacles:
def __init__(self,stars,tyre,rock,canvas,rockX,rockY,starX1,starX2,starY1,starY2,tyreX,tyreY):
self.stars = stars
self.tyre = tyre
self.rock = rock
self.canvas = canvas
self.rockX = rockX
self.rockY = rockY
self.starX1 = starX1
self.starX2 = starX2
self.starY1 = starY1
self.starY2 = starY2
self.tyreX = tyreX
self.tyreY = tyreY
# self.rand = rand
def choose(self):
self.rand = random.randint(1,3)
if self.rand == 1:
self.rockImage()
if self.rand == 2:
self.starsImage()
if self.rand == 3:
self.tyreImage()
def rockImage(self):
self.canvas.blit(self.rock, (self.rockX,self.rockY))
self.rockX -= 1
if self.rockX == 0:
self.choose()
def starsImage(self):
self.canvas.blit(self.stars[0], (self.starX1,self.starY1))
self.canvas.blit(self.stars[1], (self.starX2,self.starY2))
self.starX1 -= 1
self.starX2 -= 1
if self.starX1 == 0 and self.starX2 == 0:
self.choose()
def tyreImage(self):
self.canvas.blit(self.tyre, (self.tyreX,self.tyreY))
self.tyreX -= 1
if self.tyreX == 0:
self.choose()
tim = 10
class Ninja:
def __init__(self,canvas,start,run,image,x,y,time): #Initializing of variables
self.canvas = canvas
self.start = start
self.run = run
self.image = image
self.x = x
self.y = y
self.time = time
def ninja(self): #ninja movement method
self.image = ninjaRun[0]
self.sec = int((time.time()-self.start)*self.time)
if self.sec % 2 != 0:
self.image = ninjaRun[0]
elif self.sec % 2 == 0:
self.image = ninjaRun[1]
self.canvas.blit(self.image, (self.x,self.y)) #ninja draw
player = Ninja(canvas,start,ninjaRun,ninjaRun[0],x,y,tim) #calling Ninja class
obstacle = obstacles(ninjaStars,tyre,rock,canvas,rockX,rockY,starX1,starX2,starY1,starY2,tyreX,tyreY)#obstacle class
cnt = 0
startTime = time.time()
count = 1
while True: #main loop
if player.y == y:
ninjaState = "Run"
elif player.y != y:
ninjaState = "Jump"
# print(player.time)
for event in pygame.event.get(): #event handler
if event.type == pygame.QUIT: #quit handler
pygame.quit()
import sys
sys.exit()
if event.type == pygame.KEYDOWN: #keydown
if event.key == pygame.K_SPACE:
count += 1
if ninjaState == "Jump":
pass
elif ninjaState == "Run":
player.y -= 300
if event.type == pygame.KEYUP: #keyup
if event.key == pygame.K_SPACE:
spaceEnd = pygame.time.get_ticks()/1000
player.y += 300
canvas.blit(sky, (0,0))#sky drawing
canvas.blit(land, (0,718))#land drawing
player.ninja() #ninja drawing
obstacle.choose()
pygame.display.update() #display update
clock.tick(100)
Any answer will be appreciated. Thanks
Aucun commentaire:
Enregistrer un commentaire