def renderItems(self, renderer): for x in range(len(self.items)): spot = sdl.Rect((10 + x * 55, 65, 50, 50)) if self.items[x] is 'key': sdl.renderCopy(renderer, self.textures[1][1], None, spot) elif self.items[x] is 'carrot': sdl.renderCopy(renderer, self.textures[1][2], None, spot)
def renderDamage(self, renderer, xMin, yMin): #if there has been any change in health if (self.changeInHp != 0): #create the string stringDamage = str(self.changeInHp) #make it red color = sdl.Color((255, 0, 0)).cdata[0] #but if the "damage" is positive (ie, healing) if (self.changeInHp > 0): #show that it is positive stringDamage = "+" + str(self.changeInHp) #and make it green color = sdl.Color((0, 255, 0)).cdata[0] dmgSurf = sdl.ttf.renderText_Solid(driver.scoreFont, stringDamage, color) dmgTexture = renderer.createTextureFromSurface(dmgSurf) textureW, textureH = chef.textureSize(dmgTexture) dmgRect = sdl.Rect() dmgRect.x = int(self.xPos + textureW // 6 - xMin) dmgRect.y = int(self.yPos - textureH - yMin) dmgRect.w = textureW dmgRect.h = textureH dmgRect.x = int(dmgRect.x) dmgRect.y = int(dmgRect.y) sdl.renderCopy(renderer, dmgTexture, None, dmgRect) sdl.freeSurface(dmgSurf) sdl.destroyTexture(dmgTexture)
def run(self): running = True currentTime = 0 lastTime = 0 time = 0 event = sdl.Event() while running: sdl.renderClear(driver.renderer) textureW, textureH = game.textureSize(backgroundTexture) backgroundRect = game.centeredRect(game.width, game.height, textureW, textureH) sdl.renderCopy(driver.renderer, backgroundTexture, None, backgroundRect) while sdl.pollEvent(event): if event.type == sdl.MOUSEBUTTONDOWN: running = False self.next = "menu" elif event.type == sdl.KEYDOWN: if event.key.keysym.sym in [sdl.K_q, sdl.K_ESCAPE]: running = False game.turnOff() elif event.key.keysym.sym in [sdl.K_SPACE, sdl.K_RETURN]: running = False self.next = "menu" sdl.renderPresent(driver.renderer)
def run(self): running = True count = 0 event = sdl.Event() #if self.num is 7: while running: sdl.renderClear(driver.renderer) sdl.renderCopy(driver.renderer, self.textures[count], None, textureRect) if count == 0 and self.num is 7: self.updateAlpha() sdl.setTextureAlphaMod(blackScreen, int(self.alpha)) sdl.renderCopy(driver.renderer, blackScreen, None, textureRect) while sdl.pollEvent(event): if event.type == sdl.QUIT: running = False game.turnOff() elif event.type == sdl.MOUSEBUTTONDOWN: count += 1 elif event.type == sdl.KEYUP: if event.key.keysym.sym in [sdl.K_q, sdl.K_ESCAPE]: running = False game.turnOff() elif event.key.keysym.sym == sdl.K_RETURN: count += 100 elif (count < self.length - 1 or self.num is 2 or self.num is 4 or self.num is 6 or self.num is 7) and event.key.keysym.sym == sdl.K_SPACE: count += 1 if count > self.length - 1: running = False sdl.renderPresent(driver.renderer)
def renderSelf(self, time, renderer, xMin, yMin): if (self.xPos + self.width - xMin < 0) or (self.yPos + self.height - yMin < 0): return elif (self.xPos > xMin + 800) or (self.yPos > yMin + 600): return if self.title is 'pizza' or self.title is 'heart' or self.title is 'carrot' or self.title is 'bombObject': time1 = ((int(time / 250)) % 4) #slowish spriteFrame = sdl.Rect((time1 * 50, 0, 50, 50)) sdl.renderCopy(renderer, self.texture, spriteFrame, self.getRect(xMin, yMin)) elif self.title is 'explosion': time1 = ((int(time / 250)) % 8) if time1 is not self.currentTime: self.currentFrame += 1 spriteFrame = sdl.Rect((self.currentFrame * 200, 0, 200, 200)) sdl.renderCopy(renderer, self.texture, spriteFrame, self.getRect(xMin, yMin)) if self.currentFrame is 8: self.used = True self.currentTime = time1 else: spriteFrame = sdl.Rect((self.currentFrame * 200, 0, 200, 200)) sdl.renderCopy(renderer, self.texture, spriteFrame, self.getRect(xMin, yMin)) elif self.title is 'blackScreen': self.updateAlpha(1) sdl.setTextureAlphaMod(self.texture, int(self.alpha)) sdl.renderCopy(renderer, self.texture, None, self.getRect(xMin, yMin)) else: sdl.renderCopy(renderer, self.texture, None, self.getRect(xMin, yMin))
def renderSelf(self, time, xMin, yMin): if self.title is 'green': currentFrame = ((int(time / 125)) % 6) spriteFrame = sdl.Rect((currentFrame * 60, 0, 60, 60)) sdl.renderCopy(self.renderer, self.texture, spriteFrame, self.getRect(xMin, yMin)) elif self.title is 'purple': newTime = ((int(time / 400)) % 6) if newTime is not self.currentTime: self.currentFrame += 1 if self.currentFrame > 5: self.currentFrame = 5 spriteFrame = sdl.Rect((self.currentFrame * 400, 0, 400, 200)) sdl.renderCopy(self.renderer, self.texture, spriteFrame, self.getRect(xMin, yMin)) elif self.title is 'knife': tempRect = self.getRect(xMin, yMin) sdl.renderCopyEx(self.renderer, self.texture, None, tempRect, math.degrees(self.degree) + 90, sdl.Point((tempRect.w // 2, tempRect.h // 2)), sdl.FLIP_NONE) elif self.title is 'fire' or self.title is 'fireSlow': currentFrame = ((int(time / 125)) % 4) spriteFrame = sdl.Rect((currentFrame * 50, 0, 50, 150)) tempRect = self.getRect(xMin, yMin) sdl.renderCopyEx(self.renderer, self.texture, spriteFrame, tempRect, math.degrees(self.degree) - 90, sdl.Point((tempRect.w // 2, tempRect.h // 2)), sdl.FLIP_NONE)
def renderLight(self, renderer, xMin, yMin): mod = 1 texture = driver.lightTextures[7] # default red if self.attacking is 1: texture = driver.lightTextures[6] # change to green sdl.setTextureAlphaMod(texture, 64) mod = 10 elif self.attacking is 2: texture = driver.lightTextures[3] # change to orange sdl.setTextureAlphaMod(texture, 64) elif self.attacking is 3: texture = driver.lightTextures[4] # change to purple sdl.setTextureAlphaMod(texture, 64) if self.hurt: texture = driver.lightTextures[5] # change to blue sdl.setTextureAlphaMod(texture, 64) if self.dying: color = (self.currentFrame % 5) + 3 texture = driver.lightTextures[ color] # change to self.currentFrame % len(driver.lightTextures) sdl.setTextureAlphaMod(texture, 255) if color is 6: mod = 10 textureW, textureH = chef.textureSize(texture) textureW *= mod textureH *= mod lightRect = sdl.Rect((0, 0, textureW, textureH)) sdl.setTextureBlendMode(texture, sdl.BLENDMODE_ADD) posRect = sdl.Rect( (int(self.xPos) - int(xMin) + int(self.width / 2) - int(textureW / 2), int(self.yPos) - int(yMin) + int(self.height / 2) - int(textureH / 2), textureW, textureH)) sdl.renderCopy(renderer, texture, lightRect, posRect)
def renderHealth(self, renderer, xMin, yMin): for i in range(self.health): spot = sdl.Rect( (int(self.xPos) - int(xMin) + 14 * i - int(14 * (self.health / 2)) + int(self.width / 2), int(self.yPos) - int(yMin) + int(self.height) - 10, 12, 12)) sdl.renderCopy(renderer, self.bossTextures[11], None, spot)
def run(self): sdl.renderClear(driver.renderer) textureW, textureH = game.textureSize(loadingTexture) textureRect = game.centeredRect(game.width, game.height, textureW, textureH) sdl.renderCopy(driver.renderer, loadingTexture, None, textureRect) sdl.renderPresent(driver.renderer)
def renderHealthPvp(self, renderer): if self.title is None: for i in range(self.health): spot = sdl.Rect((10 + i * 55, 10, 50, 50)) sdl.renderCopy(renderer, self.textures[1][0], None, spot) else: for i in range(self.health): spot = sdl.Rect(((16 * 50) - 65 - i * 55, 10, 50, 50)) sdl.renderCopy(renderer, self.textures[1][0], None, spot)
def renderScore(self, renderer): stringScore = "Score: " + str(self.totalScore) score = sdl.ttf.renderText_Solid(driver.scoreFont, stringScore, sdl.Color((255, 255, 255)).cdata[0]) scoreTexture = renderer.createTextureFromSurface(score) textureW, textureH = game.textureSize(scoreTexture) scoreRect = game.centeredRect(game.width, 60, textureW, textureH) sdl.renderCopy(renderer, scoreTexture, None, scoreRect) sdl.freeSurface(score) sdl.destroyTexture(scoreTexture)
def renderLight(self, renderer, xMin, yMin): if 'carrot' in driver.chef.items: textureW, textureH = chef.textureSize(driver.lightTextures[0]) lightRect = sdl.Rect((0, 0, textureW, textureH)) sdl.setTextureBlendMode(driver.lightTextures[0], sdl.BLENDMODE_ADD) sdl.setTextureAlphaMod(driver.lightTextures[0], 128) posRect = sdl.Rect( (int(self.xPos) - int(xMin) + int(self.width / 2) - int(textureW / 2), int(self.yPos) - int(yMin) + int(self.height / 2) - int(textureH / 2), textureW, textureH)) sdl.renderCopy(renderer, driver.lightTextures[0], lightRect, posRect)
def overlayblack(self): if 'carrot' not in driver.chef.items: blackRect = sdl.Rect( (int(self.chef.xPos - 800) - int(self.xMin), (self.chef.yPos - 600) - int(self.yMin), 1600, 1200)) else: blackRect = sdl.Rect( (int(self.chef.xPos - 1200) - int(self.xMin), (self.chef.yPos - 900) - int(self.yMin), 2400, 1800)) if self.level is 5: blackRect = sdl.Rect( (int(self.chef.xPos - 1800) - int(self.xMin), (self.chef.yPos - 1350) - int(self.yMin), 3600, 2700)) sdl.renderCopy(driver.renderer, self.blackScreenTexture, None, blackRect)
def renderLight(self, renderer, xMin, yMin): for particle in self.particles: if self.type is "explosion": if not particle.isAlive(): self.particles.remove(particle) else: expRect = sdl.Rect((0, 0, 10, 10)) pixSize = particle.getTimeFraction() pixSize = (pixSize**3) // 2 sdl.renderCopy( renderer, self.expLight, None, sdl.Rect( (int(particle.xPos) - int(xMin) - int(pixSize / 2), int(particle.yPos) - int(yMin) - int(pixSize / 2), int(pixSize), int(pixSize))))
def renderLight(self, renderer, xMin, yMin): return textureW, textureH = chef.textureSize(driver.lightTextures[1]) lightRect = sdl.Rect((0, 0, textureW, textureH)) sdl.setTextureBlendMode(driver.lightTextures[1], sdl.BLENDMODE_ADD) if 'carrot' not in driver.chef.items: posRect = sdl.Rect( (int(self.xPos) - int(xMin) + int(self.width / 2) - int(textureW / 2), int(self.yPos) - int(yMin) + int(self.height / 2) - int(textureH / 2), textureW, textureH)) else: posRect = sdl.Rect( (int(self.xPos) - int(xMin) + int(self.width / 2) - int(textureW), int(self.yPos) - int(yMin) + int(self.height / 2) - int(textureH), textureW * 2, textureH * 2)) sdl.renderCopy(renderer, driver.lightTextures[1], lightRect, posRect)
def renderLight(self, renderer, xMin, yMin): if (self.identity is "redPipe" or self.identity is "bluePipe" ) and self.used is False and 'carrot' in driver.chef.items: if self.identity is "redPipe": texture = driver.lightTextures[7] else: texture = driver.lightTextures[5] textureW = 100 textureH = 100 sdl.setTextureBlendMode(texture, sdl.BLENDMODE_ADD) sdl.setTextureAlphaMod(texture, 128) xP = int(self.xPos * 50) - int(xMin) + 25 - int(textureW / 2) yP = int(self.yPos * 50) - int(yMin) + 25 - int(textureH / 2) posRect = sdl.Rect((xP, yP, textureW, textureH)) sdl.renderCopy(renderer, texture, None, posRect) else: return
def run(self): running = True currentTime = 0 lastTime = 0 time = 0 pressed = False event = sdl.Event() while running: currentTime = sdl.getTicks() dt = currentTime - lastTime time += dt lastTime = currentTime textureW, textureH = game.textureSize(winTexture) goRect = game.centeredRect(game.width, game.height, textureW, textureH) if time <= 3000: sdl.renderClear(driver.renderer) alpha = int(round((time / 3000.0) * 255)) sdl.setTextureAlphaMod(winTexture, alpha) sdl.renderCopy(driver.renderer, winTexture, None, goRect) while sdl.pollEvent(event): if event.type == sdl.KEYDOWN: time += 3000 if event.key.keysym.sym in [sdl.K_q, sdl.K_ESCAPE]: running = False game.turnOff() else: sdl.renderClear(driver.renderer) sdl.renderCopy(driver.renderer, winTexture, None, goRect) while sdl.pollEvent(event): if event.type == sdl.KEYDOWN: if event.key.keysym.sym in [sdl.K_q, sdl.K_ESCAPE]: running = False game.turnOff() elif event.key.keysym.sym in [ sdl.K_SPACE, sdl.K_RETURN ]: running = False self.next = "highScore" sdl.renderPresent(driver.renderer)
def renderWeapons(self, renderer): for x in range(len(self.weapons)): spot = sdl.Rect((10 + x * 55, 120, 50, 50)) if self.weapons[x] is 'ladles': sdl.renderCopy(renderer, self.textures[1][3], None, spot) if self.weapons[x] is 'graters': sdl.renderCopy(renderer, self.textures[1][4], None, spot) if self.weapons[x] is 'bomb': sdl.renderCopy(renderer, self.textures[1][5], None, spot) if self.weapons[x] is 'knives': sdl.renderCopy(renderer, self.textures[1][9], None, spot)
def renderLight(self, renderer, xMin, yMin): # textureW, textureH = chef.textureSize(driver.lightTextures[1]) # lightRect = sdl.Rect((0, 0, textureW, textureH)) # sdl.setTextureBlendMode(driver.lightTextures[1], sdl.BLENDMODE_ADD) # sdl.setTextureAlphaMod(driver.lightTextures[1], 128) # posRect = sdl.Rect((int(self.xPos) - int(xMin) + int(self.width/2) - int(textureW/2) ,int(self.yPos) - int(yMin) + int(self.height/2) - int(textureH/2), textureW, textureH)) # sdl.renderCopy(renderer, driver.lightTextures[1], lightRect, posRect) mod = 1 texture = driver.lightTextures[5] # default blue if self.hurt: texture = driver.lightTextures[7] # change to red sdl.setTextureAlphaMod(texture, 255) elif self.summoning: texture = driver.lightTextures[6] # change to green sdl.setTextureAlphaMod(texture, 64) mod = 10 elif self.transforming: texture = driver.lightTextures[3] # change to orange sdl.setTextureAlphaMod(texture, 64) elif self.transformed: texture = driver.lightTextures[7] # change to red sdl.setTextureAlphaMod(texture, 64) if self.dying: color = (self.currentFrame % 5) + 3 texture = driver.lightTextures[color] sdl.setTextureAlphaMod(texture, 255) if color is 6: mod = 10 textureW, textureH = chef.textureSize(texture) textureW *= mod textureH *= mod lightRect = sdl.Rect((0, 0, textureW, textureH)) sdl.setTextureBlendMode(texture, sdl.BLENDMODE_ADD) posRect = sdl.Rect( (int(self.xPos) - int(xMin) + int(self.width / 2) - int(textureW / 2), int(self.yPos) - int(yMin) + int(self.height / 2) - int(textureH / 2), textureW, textureH)) sdl.renderCopy(renderer, texture, lightRect, posRect)
def renderMessage(self, renderer, xMin, yMin): if self.used is False: if self.identity is "pantry" or self.identity is "graters": sdl.renderCopy(renderer, self.texture[2], None, self.getMessageRect(2)) elif self.identity is "stairs": sdl.renderCopy(renderer, self.texture[5], None, self.getMessageRect(5)) elif self.identity is "bluePipe": sdl.renderCopy(renderer, self.texture[7], None, self.getMessageRect(7)) elif self.identity is "redPipe": sdl.renderCopy(renderer, self.texture[8], None, self.getMessageRect(8))
def run(self): running = True currentTime = 0 lastTime = 0 time = 0 event = sdl.Event() textureW, textureH = game.textureSize(full) textureRect = game.centeredRect(game.width, game.height, textureW, textureH) sdl.setTextureBlendMode(full, sdl.BLENDMODE_ADD) while running: currentTime = sdl.getTicks() dt = currentTime - lastTime time += dt lastTime = currentTime if time <= 3000: sdl.renderClear(driver.renderer) alpha = int(round((time / 3000.0) * 255)) if alpha > 255: time += 3000 alpha = 255 sdl.renderCopy(driver.renderer, fade, None, textureRect) sdl.setTextureAlphaMod(full, alpha) sdl.renderCopy(driver.renderer, full, None, textureRect) while sdl.pollEvent(event): if event.type == sdl.KEYDOWN: time += 3000 elif event.type == sdl.MOUSEBUTTONDOWN: time += 3000 if event.key.keysym.sym in [sdl.K_q, sdl.K_ESCAPE]: running = False game.turnOff() else: sdl.renderCopy(driver.renderer, fade, None, textureRect) sdl.renderCopy(driver.renderer, full, None, textureRect) while sdl.pollEvent(event): if event.type == sdl.MOUSEBUTTONDOWN: running = False self.next = "menu" if event.type == sdl.KEYDOWN: if event.key.keysym.sym in [sdl.K_q, sdl.K_ESCAPE]: running = False game.turnOff() else: running = False self.next = "menu" sdl.renderPresent(driver.renderer)
def Draw(s): viewport = sdl.Rect() sdl.renderGetViewport(s.renderer, viewport) # Draw the background sdl.renderCopy(s.renderer, s.background, None, None) # Scale and draw the sprite s.sprite_rect.w += s.scale_direction s.sprite_rect.h += s.scale_direction if s.scale_direction > 0: if s.sprite_rect.w >= viewport.w or s.sprite_rect.h >= viewport.h: s.scale_direction = -1 else: if s.sprite_rect.w <= 1 or s.sprite_rect.h <= 1: s.scale_direction = 1 s.sprite_rect.x = (viewport.w - s.sprite_rect.w) // 2 s.sprite_rect.y = (viewport.h - s.sprite_rect.h) // 2 sdl.renderCopy(s.renderer, s.sprite, ffi.NULL, s.sprite_rect) # Update the screen! sdl.renderPresent(s.renderer)
def renderLight(self, renderer, xMin, yMin): texture = driver.lightTextures[7] modX = 1 modY = 1 if self.title is 'green': texture = driver.lightTextures[6] # change to green elif self.title is 'purple': texture = driver.lightTextures[4] # change to purple modX = 1 modY = 1 elif self.title is 'knife': texture = driver.lightTextures[1] # change to green return textureW, textureH = chef.textureSize(texture) textureW = textureW / modX textureH = textureH / modY lightRect = sdl.Rect((0, 0, int(textureW), int(textureH))) sdl.setTextureBlendMode(texture, sdl.BLENDMODE_ADD) sdl.setTextureAlphaMod(texture, 128) posRect = sdl.Rect((int(self.xPos) - int(xMin) + int(self.width / 2) - int(textureW / 2), int(self.yPos) - int(yMin) + int(self.height / 2) - int(textureH / 2), int(textureW), int(textureH))) sdl.renderCopy(renderer, texture, None, posRect)
def renderSelf(self, renderer, xMin, yMin, dt): for particle in self.particles: if self.type is "rat": particle.update(dt) if not particle.isAlive(): self.particles.remove(particle) else: pixSize = particle.getTimeFraction() sdl.renderCopy( renderer, self.tex, None, sdl.Rect((int(particle.xPos) - int(xMin), int(particle.yPos) - int(yMin), pixSize, pixSize))) elif self.type is "explosion": particle.update(dt) if not particle.isAlive(): self.particles.remove(particle) else: pixSize = particle.getTimeFraction() if particle.life > 900: sdl.renderCopy( renderer, self.tex3, None, sdl.Rect((int(particle.xPos) - int(xMin), int(particle.yPos) - int(yMin), pixSize, pixSize))) elif particle.life > 800: sdl.renderCopy( renderer, self.tex2, None, sdl.Rect((int(particle.xPos) - int(xMin), int(particle.yPos) - int(yMin), pixSize, pixSize))) elif particle.life > 0: sdl.renderCopy( renderer, self.tex1, None, sdl.Rect((int(particle.xPos) - int(xMin), int(particle.yPos) - int(yMin), pixSize, pixSize)))
def run(self): cursor1 = MC.Chef(220, 190, 50, 80, 'none', 0, 0, 0, 0.001, 0.001, cursorTexture, True, 0, 0, 100, True, 1) cursor2 = MC.Chef(535, 190, 50, 80, 'none', 0, 0, 0, 0.001, 0.001, cursorTexture, True, 0, 0, 100, True, 1) running = True currentTime = 0 lastTime = 0 time = 0 cursorPos = 0 brightnessPos = 10 volumePos = round((driver.volume - 8) / 12) event = sdl.Event() textureW, textureH = game.textureSize(optionsTexture) textureRect = game.centeredRect(game.width, game.height, textureW, textureH) sdl.renderClear(driver.renderer) sdl.renderCopy(driver.renderer, optionsTexture, None, textureRect) # render cursor in time1 = ((int(time / 125)) % 4) time2 = ((int(time / 500)) % 4) # the rectangle that defines which sprite part the sprites spriteFrame = sdl.Rect((time1 * 48, 0, 48, 80)) spriteFrame2 = sdl.Rect((time2 * 48, 0, 48, 80)) sdl.renderCopy(driver.renderer, cursorTexture, spriteFrame2, cursor1.getRect(0, 0)) sdl.renderCopy(driver.renderer, cursorTexture, spriteFrame2, cursor2.getRect(0, 0)) #render in brightness indicators for i in range(brightnessPos): spot = sdl.Rect((280 + 25 * i, 357, 18, 18)) sdl.renderCopy(driver.renderer, indTexture, None, spot) #render in volume indicators for i in range(volumePos): spot = sdl.Rect((280 + 25 * i, 242, 18, 18)) sdl.renderCopy(driver.renderer, indTexture, None, spot) sdl.renderPresent(driver.renderer) while running: hover = None clicked = None #### TIME MANAGEMENT #### currentTime = sdl.getTicks() dt = currentTime - lastTime time += dt lastTime = currentTime #### TEXTURE MEASUREMENTS #### textureW, textureH = game.textureSize(optionsTexture) textureRect = game.centeredRect(game.width, game.height, textureW, textureH) #### HANDLE INPUT #### while sdl.pollEvent(event): if event.type == sdl.QUIT: running = False game.turnOff() elif event.type == sdl.KEYUP: if event.key.keysym.sym in [sdl.K_q, sdl.K_ESCAPE]: running = False game.turnOff() elif event.type == sdl.MOUSEBUTTONDOWN: if event.button.button == sdl.BUTTON_LEFT: for i in range(len(boxes)): if clicked is None: sdl.mixer.playChannel(0, selectSound, 0) clicked = boxes[i].checkEvent( event.motion.x, event.motion.y) for i in range(len(volumeBoxes)): here = volumeBoxes[i].checkEvent( event.motion.x, event.motion.y) if here is not None: volumePos = here elif event.type == sdl.MOUSEMOTION: for i in range(len(boxes)): if (boxes[i].checkMotion(event.motion.x, event.motion.y)): hover = boxes[i].cursorPos if event.type == sdl.KEYDOWN: if event.key.keysym.sym == sdl.K_UP: sdl.mixer.playChannel(1, scrollSound, 0) cursorPos -= 1 if cursorPos <= -1: cursorPos = 2 elif event.key.keysym.sym == sdl.K_DOWN: sdl.mixer.playChannel(1, scrollSound, 0) cursorPos = (cursorPos + 1) % 3 elif event.key.keysym.sym == sdl.K_RIGHT: sdl.mixer.playChannel(1, scrollSound, 0) if cursorPos is 1: if sdl.getWindowBrightness(driver.window) < 1: brightnessPos += 1 sdl.setWindowBrightness( driver.window, sdl.getWindowBrightness(driver.window) + 0.05) elif cursorPos is 0: if volumePos < 10: volumePos += 1 sdl.mixer.volume(-1, volumePos * 12) driver.volume = volumePos * 12 elif event.key.keysym.sym == sdl.K_LEFT: sdl.mixer.playChannel(1, scrollSound, 0) if cursorPos is 1: if sdl.getWindowBrightness(driver.window) > .5: brightnessPos -= 1 sdl.setWindowBrightness( driver.window, sdl.getWindowBrightness(driver.window) - 0.05) elif cursorPos is 0: if volumePos > 1: volumePos -= 1 sdl.mixer.volume(-1, volumePos * 12) driver.volume = volumePos * 12 elif event.key.keysym.sym == sdl.K_RETURN: if (cursorPos == 0): # Call to play game sdl.mixer.playChannel(1, scrollSound, 0) elif (cursorPos == 1): #ADD IN OPTIONS sdl.mixer.playChannel(1, scrollSound, 0) elif (cursorPos == 2): #Call to controls screen sdl.mixer.playChannel(0, selectSound, 0) running = False self.next = "menu" while (sdl.getTicks() - currentTime < 300): pass if (hover is not None): cursorPos = hover if clicked is not None: if cursorPos is 2: sdl.mixer.playChannel(0, selectSound, 0) running = False self.next = "menu" cursor1.updateY(190 + 115 * cursorPos) cursor2.updateY(190 + 115 * cursorPos) #### RENDER #### sdl.renderClear(driver.renderer) sdl.renderCopy(driver.renderer, optionsTexture, None, textureRect) # render cursor in time1 = ((int(time / 125)) % 4) time2 = ((int(time / 500)) % 4) # the rectangle that defines which sprite part the sprites spriteFrame = sdl.Rect((time1 * 48, 0, 48, 80)) spriteFrame2 = sdl.Rect((time2 * 48, 0, 48, 80)) sdl.renderCopy(driver.renderer, cursorTexture, spriteFrame2, cursor1.getRect(0, 0)) sdl.renderCopy(driver.renderer, cursorTexture, spriteFrame2, cursor2.getRect(0, 0)) for i in range(brightnessPos): spot = sdl.Rect((280 + 25 * i, 357, 18, 18)) sdl.renderCopy(driver.renderer, indTexture, None, spot) for i in range(volumePos): spot = sdl.Rect((280 + 25 * i, 242, 18, 18)) sdl.renderCopy(driver.renderer, indTexture, None, spot) sdl.renderPresent(driver.renderer)
def renderHealth(self, renderer, xMin, yMin, texture): for i in range(self.health): spot = sdl.Rect( (int(self.xPos) - int(xMin) + 14 * i, int(self.yPos) - int(yMin) + int(self.height) - 30, 12, 12)) sdl.renderCopy(renderer, texture, None, spot)
def renderSelf(self, time, renderer, xMin, yMin, bossTextures): if (self.xPos + self.width - xMin < 0) or (self.yPos + self.height - yMin < 0): return elif (self.xPos > xMin + 800) or (self.yPos > yMin + 600): return frameTime = ((int(time / 125)) % 4) spriteFrame = sdl.Rect((frameTime * 300, 0, 300, 400)) if self.dead: return if self.dying: frameTime = ((int(time / 500)) % 4) if self.currentTime is not frameTime: self.currentFrame += 1 self.currentTime = frameTime spriteFrame = sdl.Rect((self.currentFrame * 300, 0, 300, 400)) sdl.renderCopy(renderer, bossTextures[9], spriteFrame, self.getRect(xMin, yMin)) # once the animation for attacking runs through, set him back to not attacking # and reset the current frame to -1 (so it starts at frame 0 next run) if self.currentFrame is 10: self.currentFrame = -1 self.attacking = 0 self.currentTime = -1 self.dead = True self.dying = False else: self.currentTime = frameTime spriteFrame = sdl.Rect((self.currentFrame * 300, 0, 300, 400)) sdl.renderCopy(renderer, bossTextures[9], spriteFrame, self.getRect(xMin, yMin)) elif not self.hurt: # if not hurt if self.attacking is 0: # if not attacking if self.direction == 'e': sdl.renderCopy(renderer, bossTextures[1], spriteFrame, self.getRect(xMin, yMin)) #right elif self.direction == 'w': sdl.renderCopy(renderer, bossTextures[2], spriteFrame, self.getRect(xMin, yMin)) #left elif self.direction == 's': sdl.renderCopy(renderer, bossTextures[8], spriteFrame, self.getRect(xMin, yMin)) #idle else: # if attacking if self.attacking == 1: # GREEN ATTACK frameTime = ((int(time / 125)) % 4) if self.currentTime is not frameTime: self.currentFrame += 1 self.currentTime = frameTime spriteFrame = sdl.Rect( (self.currentFrame * 300, 0, 300, 400)) sdl.renderCopy(renderer, bossTextures[3], spriteFrame, self.getRect(xMin, yMin)) if self.currentFrame is 4: self.projectiles.append( Projectile( int(self.xPos) + 100, int(self.yPos) + 400, 60, 60, 'green', bossTextures[4], renderer, 0, 0, 0)) # once the animation for attacking runs through, set him back to not attacking # and reset the current frame to -1 (so it starts at frame 0 next run) if self.currentFrame >= 6: self.currentFrame = -1 self.attacking = 0 self.currentTime = -1 else: self.currentTime = frameTime spriteFrame = sdl.Rect( (self.currentFrame * 300, 0, 300, 400)) sdl.renderCopy(renderer, bossTextures[3], spriteFrame, self.getRect(xMin, yMin)) elif self.attacking == 2: # ORANGE ATTACK frameTime = ((int(time / 125)) % 4) if self.currentTime is not frameTime: self.currentFrame += 1 self.currentTime = frameTime spriteFrame = sdl.Rect( (self.currentFrame * 700, 0, 700, 800)) sdl.renderCopy(renderer, bossTextures[5], spriteFrame, self.getRect(xMin, yMin)) # once the animation for attacking runs through, set him back to not attacking # and reset the current frame to -1 (so it starts at frame 0 next run) if self.currentFrame >= 8: self.currentFrame = -1 self.attacking = 0 self.currentTime = -1 self.endOfOrange = True else: self.currentTime = frameTime spriteFrame = sdl.Rect( (self.currentFrame * 700, 0, 700, 800)) sdl.renderCopy(renderer, bossTextures[5], spriteFrame, self.getRect(xMin, yMin)) elif self.attacking == 3: # PURPLE ATTACK frameTime = ((int(time / 125)) % 4) if self.currentTime is not frameTime: self.currentFrame += 1 self.currentTime = frameTime spriteFrame = sdl.Rect( (self.currentFrame * 300, 0, 300, 400)) sdl.renderCopy(renderer, bossTextures[6], spriteFrame, self.getRect(xMin, yMin)) if self.currentFrame is 4: self.projectiles.append( Projectile( int(self.xPos) - 100, int(self.yPos) + 400, 400, 200, 'purple', bossTextures[7], renderer, 0, 0, 0)) # once the animation for attacking runs through, set him back to not attacking # and reset the current frame to -1 (so it starts at frame 0 next run) if self.currentFrame >= 6: self.currentFrame = -1 self.attacking = 0 self.currentTime = -1 else: self.currentTime = frameTime spriteFrame = sdl.Rect( (self.currentFrame * 300, 0, 300, 400)) sdl.renderCopy(renderer, bossTextures[6], spriteFrame, self.getRect(xMin, yMin)) if self.attacking == 4: sdl.renderCopy(renderer, bossTextures[8], spriteFrame, self.getRect(xMin, yMin)) elif self.hurt: # if hurt sdl.renderCopy(renderer, bossTextures[0], spriteFrame, self.getRect(xMin, yMin)) self.renderHealth(renderer, xMin, yMin, bossTextures[10])
def copy(self, texture, srcrect=None, dstrect=None): assert self.handle is not None assert isinstance(texture, Texture) and texture.handle is not None if sdl.renderCopy(self.handle, texture.handle, srcrect, dstrect) != 0: raise SDLException("Could not render texture")
def run(self, itemName): sdl.mixer.volume(-1, driver.volume) sdl.mixer.volumeMusic(driver.volume) running = True currentTime = 0 lastTime = 0 time = 0 keepOnPlaying = True event = sdl.Event() sdl.mixer.allocateChannels(2) selectSound = sdl.mixer.loadWAV_RW( sdl.RWFromFile("music//chunks/menu_select.wav", 'rw'), 0) cur = 0 if itemName is "bsv": cur = 0 elif itemName is "key": cur = 1 elif itemName is "ladles": cur = 2 elif itemName is "graters": cur = 3 elif itemName is "carrot": cur = 4 elif itemName is "pizza": cur = 5 elif itemName is "tomato": cur = 6 elif itemName is "knives": cur = 7 while running: #### TIME MANAGEMENT #### currentTime = sdl.getTicks() dt = currentTime - lastTime time += dt lastTime = currentTime #### TEXTURE MEASUREMENTS #### textureW, textureH = game.textureSize(driver.chef.textures[3][cur]) textureRect = game.centeredRect(game.width, game.height, textureW, textureH) sdl.renderClear(driver.renderer) self.camera.display(time, dt, False, None) sdl.renderCopy(driver.renderer, driver.chef.textures[3][cur], None, textureRect) sdl.renderPresent(driver.renderer) #### HANDLE INPUT #### while sdl.pollEvent(event): if event.type == sdl.QUIT: running = False game.turnOff() keepOnPlaying = False elif event.type == sdl.KEYUP: if event.key.keysym.sym in [sdl.K_q, sdl.K_ESCAPE]: running = False game.turnOff() keepOnPlaying = False if event.type == sdl.KEYDOWN: if event.key.keysym.sym is sdl.K_SPACE: sdl.mixer.playChannel(0, selectSound, 0) running = False keepOnPlaying = True #### RENDER #### if not running: return keepOnPlaying sdl.renderClear(driver.renderer) if selectSound is not None: sdl.mixer.freeChunk(selectSound)
def run(): running = True lastTime = 0 time = 0 up = False down = False left = False right = False event = sdl.Event() xPos = 100 yPos = 100 size = 200 while running: currentTime = sdl.getTicks() dt = currentTime - lastTime time += dt / 1000.0 lastTime = currentTime textureW, textureH = textureSize(texture) textureRect = centeredRect(width, height, textureW, textureH) #Figure out what keys are being pressed while sdl.pollEvent(event): if event.type == sdl.QUIT: running = False elif event.type == sdl.KEYUP: if event.key.keysym.sym in [sdl.K_q, sdl.K_ESCAPE]: running = False if event.key.keysym.sym == sdl.K_UP: up = False elif event.key.keysym.sym == sdl.K_DOWN: down = False elif event.key.keysym.sym == sdl.K_LEFT: left = False elif event.key.keysym.sym == sdl.K_RIGHT: right = False if event.type == sdl.KEYDOWN: if event.key.keysym.sym == sdl.K_UP: up = True elif event.key.keysym.sym == sdl.K_DOWN: down = True elif event.key.keysym.sym == sdl.K_LEFT: left = True elif event.key.keysym.sym == sdl.K_RIGHT: right = True #movement based on what keys are being pressed down if up and left: yPos -= 10 xPos -= 10 elif up and right: yPos -= 10 xPos += 10 elif down and left: yPos += 10 xPos -= 10 elif down and right: yPos += 10 xPos += 10 elif up: yPos -= 10 elif down: yPos += 10 elif left: xPos -= 10 elif right: xPos += 10 testRect = sdl.Rect((xPos, yPos, size, size)) sdl.renderClear(renderer) sdl.renderCopy(renderer, texture, None, textureRect) sdl.renderCopy(renderer, chefTexture, None, testRect) testRect = sdl.Rect((xPos, yPos, 250, 250)) sdl.renderClear(renderer) sdl.renderCopy(renderer, texture, None, textureRect) sdl.renderCopy(renderer, chefTexture, None, testRect) sdl.renderPresent(renderer)
def run(self): sdl.mixer.volume(-1, driver.volume) sdl.mixer.volumeMusic(driver.volume) cursor1 = MC.Chef(240, 160, 50, 80, 'none', 0, 0, 0, 0.001, 0.001, cursorTexture, True, 0, 0, 100, True, 1) cursor2 = MC.Chef(515, 160, 50, 80, 'none', 0, 0, 0, 0.001, 0.001, cursorTexture, True, 0, 0, 100, True, 1) running = True currentTime = 0 lastTime = 0 time = 0 cursorPos = 0 keepOnPlaying = True event = sdl.Event() controls = Controls() options = Options() while running: hover = None clicked = None #### TIME MANAGEMENT #### currentTime = sdl.getTicks() dt = currentTime - lastTime time += dt lastTime = currentTime #### TEXTURE MEASUREMENTS #### textureW, textureH = game.textureSize(pauseTexture) textureRect = game.centeredRect(game.width, game.height, textureW, textureH) #### HANDLE INPUT #### while sdl.pollEvent(event): if event.type == sdl.QUIT: running = False game.turnOff() keepOnPlaying = False elif event.type == sdl.KEYUP: if event.key.keysym.sym in [sdl.K_q, sdl.K_ESCAPE]: running = False game.turnOff() keepOnPlaying = False elif event.type == sdl.MOUSEBUTTONDOWN: if event.button.button == sdl.BUTTON_LEFT: for i in range(len(boxes)): if clicked is None: sdl.mixer.playChannel(0, selectSound, 0) clicked = boxes[i].checkEvent( event.motion.x, event.motion.y) elif event.type == sdl.MOUSEMOTION: for i in range(len(boxes)): if (boxes[i].checkMotion(event.motion.x, event.motion.y)): hover = boxes[i].cursorPos elif event.type == sdl.KEYDOWN: if event.key.keysym.sym == sdl.K_UP: sdl.mixer.playChannel(1, scrollSound, 0) cursorPos -= 1 if cursorPos <= -1: cursorPos = 3 elif event.key.keysym.sym == sdl.K_DOWN: sdl.mixer.playChannel(1, scrollSound, 0) cursorPos = (cursorPos + 1) % 4 elif event.key.keysym.sym == sdl.K_p: sdl.mixer.playChannel(0, selectSound, 0) running = False keepOnPlaying = True elif event.key.keysym.sym == sdl.K_RETURN: if (cursorPos == 0): # Call to play game sdl.mixer.playChannel(0, selectSound, 0) running = False keepOnPlaying = True elif (cursorPos == 1): #ADD IN OPTIONS sdl.mixer.playChannel(0, selectSound, 0) options.load() options.run() options.cleanup() elif (cursorPos == 2): #Call to controls screen sdl.mixer.playChannel(0, selectSound, 0) controls.load() controls.run() controls.cleanup() elif (cursorPos == 3): # Call to quit sdl.mixer.playChannel(0, selectSound, 0) running = False game.turnOff() keepOnPlaying = False while (sdl.getTicks() - currentTime < 300): pass #### RENDER #### if (clicked is not None): if clicked is 'quit': running = False game.turnOff() keepOnPlaying = False elif clicked is "controls": controls.load() controls.run() controls.cleanup() elif clicked is "options": options.load() options.run() options.cleanup() elif clicked is "resume": running = False keepOnPlaying = True if (hover is not None): cursorPos = hover cursor1.updateY(220 + 60 * cursorPos) cursor2.updateY(220 + 60 * cursorPos) sdl.renderClear(driver.renderer) self.camera.display(time, dt, False, None) sdl.renderCopy(driver.renderer, pauseTexture, None, textureRect) # render cursor in time1 = ((int(time / 125)) % 4) time2 = ((int(time / 500)) % 4) # the rectangle that defines which sprite part the sprites spriteFrame = sdl.Rect((time1 * 48, 0, 48, 80)) spriteFrame2 = sdl.Rect((time2 * 48, 0, 48, 80)) sdl.renderCopy(driver.renderer, cursorTexture, spriteFrame2, cursor1.getRect(0, 0)) sdl.renderCopy(driver.renderer, cursorTexture, spriteFrame2, cursor2.getRect(0, 0)) sdl.renderPresent(driver.renderer) if not running: return keepOnPlaying