love2d - update the text of printed text - text

I have printed text that says "Yes". and I have to buttons in the shape of arrow. I am trying to get it so that if I click the left arrow it say "No" and if I click the right arrow is says "Yes".
fsdefault = "Yes"
fs = love.graphics.print(fsdefault, 440, 160)
love.graphics.draw(larrow, 425, 163)
love.graphics.draw(rarrow, 470, 163)
function love.update(dt)
function love.mousepressed( x, y)
if x > 424 and x < 435 and y > 161 and y < 172 then
fsdefault = "No"
end
if x > 275 and x < 320 and y > 305 and y < 325 then
fsdefault = "Yes"
end
end
end

How about something like:
local fsdefault = ""
function love.mousepressed( x, y)
if x > 424 and x < 435 and y > 161 and y < 172 then
fsdefault = "No"
end
if x > 275 and x < 320 and y > 305 and y < 325 then
fsdefault = "Yes"
end
end
function love.draw()
love.graphics.print(fsdefault, 440, 160)
love.graphics.draw(larrow, 425, 163)
love.graphics.draw(rarrow, 470, 163)
end
Note that for clarity, you should only perform screen drawing operations inside love.draw.
Also, try to avoid declaring functions inside love.update. That code snippet will make love redefine love.mousepressed every single frame of your game!

Related

Parse Basic Programming

Receiving this error when trying to compile code on replit. I have already tried to declare two arrays, you need to use separate DIM statements for each array:
as 20 DIM W$(10)
30 DIM G$(26)
Error: ParseError: Parse error on line 20: Unexpected token DIM
Any suggestions on how to resolve this issue
10 REM Hangman game
20 REM Set up variables
30 DIM W$(10), G$(26)
40 LET C = 0
50 LET I = 0
60 LET J = 0
70 REM Set up word list
80 W$(1) = "HANGMAN"
90 W$(2) = "BASIC"
100 W$(3) = "COMPUTER"
110 W$(4) = "PROGRAM"
120 W$(5) = "VINTAGE"
130 REM Select a random word
140 LET R = INT(RND(1) * 5) + 1
150 LET W = W$(R)
160 REM Set up guess string
170 FOR I = 1 TO LEN(W)
180 G$(I) = "-"
190 NEXT I
200 REM Main game loop
210 DO
220 CLS
230 PRINT "Hangman"
240 PRINT
250 PRINT "Word: ";
260 FOR I = 1 TO LEN(W)
270 PRINT G$(I);
280 NEXT I
290 PRINT
300 PRINT "Guesses: ";
310 FOR I = 1 TO 26
320 IF G$(I) <> "-" THEN PRINT G$(I);
330 NEXT I
340 PRINT
350 INPUT "Enter a letter: ", L$
360 IF LEN(L$) > 1 THEN 400
370 IF L$ < "A" OR L$ > "Z" THEN 400
380 LET L = ASC(L$) - 64
390 GOTO 420
400 PRINT "Invalid input. Please enter a single letter."
410 GOTO 350
420 REM Check if letter is in word
430 LET F = 0
440 FOR I = 1 TO LEN(W)
450 IF MID$(W, I, 1) = L$ THEN G$(I) = L$: F = 1
460 NEXT I
470 IF F = 0 THEN C = C + 1
480 IF C = 6 THEN 600
490 REM Check for win
500 LET WN = 1
510 FOR I = 1 TO LEN(W)
520 IF G$(I) = "-" THEN WN = 0
530 NEXT I
540 IF WN THEN PRINT "You win!": GOTO 650
550 REM Check for loss
560 IF C = 6 THEN PRINT "You lose. The word was "; W: GOTO 650
570 LOOP
600 REM Draw hangman
610 PRINT " _____"
620 PRINT " | |"
630 IF C > 1 THEN PRINT " O |" ELSE PRINT " |"
640 IF C > 2 THEN PRINT "/|\ |" ELSE PRINT " |"
650 END
/////
I tried declaring separate arrays. I also tried running it on a vintage basic terminal, and received an error with line 150 "type mismatch"

Python - global variable inside a while loop

I have this code, where:
last_price - is a variable that gets data from a price feed.
the code, does not work as intended. it shows this output "2 test line Y", but it should show 'test line success Y'
EDIT: I cannot use "continue", because last_price is changing every second, and (Y = 'test line Y') will become (Y = 'some other command') and the code will continue execute order_succeeded_1 indefinitely.
How can I fix this code?
last_price = 230
SL = 100
SL1 = 200
SL2 = 300
SL3 = 400
X = 'yes X'
Y = 'yes Y'
Z = 'yes Z'
def addvar (A, B):
while True:
global X, Y, Z
if X == 'test line X':
print ('test line success X')
break
if Y == 'test line Y':
print ('test line success Y')
break
if Z == 'test line Z':
print ('test line success Z')
break
if SL < last_price <= SL1 : # 100 --- 200
order_succeeded = A+B # 10
X = 'test line X'
print (order_succeeded, X)
break
if SL1 < last_price <= SL2 : # 200 --- 300
order_succeeded_1 = A-B # 2
Y = 'test line Y'
print (order_succeeded_1, Y)
break
if SL2 < last_price <= SL3 : # 300 --- 400
Z = 'test line Z'
order_succeeded_2 = A * B # 24
print (order_succeeded_2, Z)
break
addvar (6, 4)
You need to use continue instead of break here, when you use break you get out of your while and Y == 'test line Y' is not executed for the second time:
if SL1 < last_price <= SL2: # 200 --- 300
order_succeeded_1 = A - B # 2
Y = 'test line Y'
print(order_succeeded_1, Y)
continue

Rainbow Animated Text

Im trying to make an animated rainbow text (left to right) so far everything was working until it reached the final of the colors list (c list) and when I try to rollback (if x == 18: ; x = 0) it dosent work
Please help
import os
import time
from sty import fg, bg, ef, rs, RgbFg
c = [196, 202, 208, 214, 220, 226, 190, 118, 121, 122, 123, 75, 33, 21, 93, 171, 201, 199]
w = "ElapsedTime"
#fg(c[0]) + w[0] + fg(c[0]) + w[1] + fg(c[0]) + w[2] + fg(c[0]) + w[3] + fg(c[0]) + w[4]
final = []
n = 0
def clear():
time.sleep(.01)
os.system("clear")
def d001(x):
y = 0
while True:
f101 = fg(c[x]) + w[y]
final.append(f101)
x = x+1
if x == 18:
x = 0
if y == 10:
break
y = y+1
print("".join(final))
final.clear()
while True:
d001(n)
print(n)
n = n+1
if n == 20:
break
X and Y both will increase by 1 and y == 10 become true before x == 18 and break the loop

how to loop through a dictionary as a range

I have a dictionary with key a start time o a video and its value as end time. each key value pair represents different group. how to check a number exists in range.
eg:
dictvid = {146: 209, 509: 539, 0: 145, 304: 320}
from 0 to 539 - 210 to 303 and 321 to 508 needs to be in another dictionary
i.e
newgroup = {210:303,321:508}
You can construct a range object from each key-value pair which will give you an O(n) solution (O(n) for iterating over the dictionary + O(1) for the in check inside each range object). Note the + 1 since range objects are exclusive in the end).
print(any(208 in range(start, end + 1) for start, end in dictvid.items()))
print(any(211 in range(start, end + 1) for start, end in dictvid.items()))
Outputs
True
False
from itertools import chain, groupby
dictvid = {146: 209, 509: 539, 0: 145, 304: 320}
i_from = 0
i_to = 539
out = dict()
s = sorted(set(range(i_from, i_to+1)).difference(chain.from_iterable(range(k, v+1) for k, v in dictvid.items())))
for _, g in groupby(enumerate(s), lambda v: v[0]-v[1]):
l = [*g]
out[l[0][1]] = l[-1][1]
print(out)
Prints:
{210: 303, 321: 508}

Using a table in Lua to display text isn't working?

I'm using Corona SDK, Sublime Text 3, and Lua. My goal in this particular program is to make a visual novel app that changes when the player clicks on the text displayed at the bottom of the screen to progress to the next text entry, therefore continuing the story.
I set up a bunch of code for logo, title screen, and other things, that works perfectly. What I'm currently trying to do within the visual novel scene is to use a table to draw the text from by change the .TEXT property to select a certain value from the table, therefore selecting the text and making that the new text. Basically, something like... (some dummy code below)
novelText = display.newText (insert the parameters for old text and the old text)
--the variable used to call the value in the table
page = 1
dummy table Novel_pages = {
[1] = nil,
[2] = "new text"
}
(insert runtime event here that calls this function)
page = page + 1
novelText.text = Novel_pages[page]
display.newText(novelText)
That was just dummy code, so please don't mind the format. :) I just want to show how I attempted to call these values from the table, and to show what I was doing without having to make people look through all my code.
So everything works fine in Corona SDK simulator, with the text even changing just momentarily -- until one second later, I get a message that reads
"mainl.lua:160: bad argument #1 to 'newText' (string expected, got table)
stack traceback:
[C]: in function 'NewText'
main.lua:160: in function <main.lua: 156>
?: in function <?169>"
Now's the part where I give you all my code! I hope it's not too much, and that I specified the problem enough. I can't see where I made the error in the table, since it should be JUST replacing the .text and not all the other properties? And then displaying with the new text properties and not have to reference the table at all afterwards? Perhaps there's a problem with needing the program to process the .text change before displaying the visual novel text...
Anyway, please help me! I would appreciate knowing what went wrong here, or being proposed an alternative! And thank you so much :)
Here's the code -- everything starts in function sceneVN()! And please excuse my cringy dialogue ingame c: It's a practice project!
local store = require( "plugin.google.iap.v3" )
local composer = require("composer")
local scene = composer.newScene()
display.setStatusBar( display.HiddenStatusBar ) -- Removes status bar
coins = 5 -- variable that defines the number of coins player has in the game.It will be different
--in a stand alone game, but as a small app meant to demonstrate function, it's necessary to start off
--with a small, defined number in the beginning.
local logo = display.newImage("/Images/logo.png", 155, 275) --code for my personal logo, drawn by me.
--Not actually showcased in the video because of time limit.
logo.alpha = 0
local function makeTitleTrue()
logo:removeSelf()
print("menu should be TRUE")
titleScreen()
end
local function fadeOut()
transition.to(logo, {time = 1000, alpha = 0, onComplete = makeTitleTrue})
end
transition.to(logo, {time = 1000, alpha = 1, onComplete = fadeOut}) -- end of logo code
function titleScreen() -- beginning of title code, which is not managed as a separate scene
title = true
titleImg = display.newImage("/Images/vn_bg.png", 155, 275)
--titleWords = display.newImage("/Images/TitleWords.png", 155, 275)
--fix to flow towards the upper right corner.
local flare = display.newImage("/Images/flare2.png", 40, 30)
flare.xScale = .5
flare.yScale = .5
local flare2 = display.newImage("/Images/flare2.png", 400, 70)
flare2.xScale = .6
flare2.yScale = .6
local flare3 = display.newImage("/Images/flare2.png", -30, 100)
flare3.xScale = .4
flare3.yScale = .4
local flare4 = display.newImage("/Images/flare2.png", 100, 400)
flare4.xScale = .4
flare4.yScale = .4
local flare5 = display.newImage("/Images/flare2.png", 400, 400)
flare5.xScale = .3
flare5.yScale = .3
local flare6 = display.newImage("/Images/flare2.png", 250, 200)
flare6.xScale = .3
flare6.yScale = .3
local function moveFlare1()
transition.to(flare, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare1})
end
local function moveFlare2()
transition.to(flare2, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare2})
end
local function moveFlare3()
transition.to(flare3, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare3})
end
local function moveFlare4()
transition.to(flare4, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare4})
end
local function moveFlare5()
transition.to(flare5, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare5})
end
local function moveFlare6()
transition.to(flare6, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare6})
end
transition.to(flare, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare1})
transition.to(flare2, {time=2500, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare2})
transition.to(flare3, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare3})
transition.to(flare4, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare4})
transition.to(flare5, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare5})
transition.to(flare6, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare6})
--add options that can when the screen is tapped, tap on an option twice to select
-- start story
-- continue story
-- coin gambling
-- end game
if (title == true) then
Runtime:addEventListener("tap", sceneVN)
end
end
function forceQuit()
function quit()
os.exit()
end
timer.performWithDelay(1000,quit)
end
function sceneVNChapter2()
return
end
function sceneVN() -- the actual visual novel code itself
display.remove(titleImg)
--display.remove(titleWords)
display.remove(flare)
display.remove(flare2)
display.remove(flare3)
display.remove(flare4)
display.remove(flare5)
display.remove(flare6)
title = false
local coinSheetData =
{
width = 32,
height = 32,
numFrames = 8,
}
local coinimageSheet = graphics.newImageSheet( "/Images/spinning_coin.png", coinSheetData )
local sequenceData =
{
name= "spinning_coin",
start = 1,
count = 8,
time = 1000,
loopCount = 0
}
--the properties of the name plate that can be changed ingame by using ".text" property
local nameOptions =
{
text = "Frankenstein",
x = 165,
y = 450,
width = 310,
font = "Charlesworth.ttf",
fontSize = 22,
align = "left"
}
local bg = display.newImage("/Images/bg4.jpg", 155, 275)
textRect = display.newRect(155, 525, 325, 200)
textRect:setFillColor(.02, .02, .02)
textRect.alpha = .6
page = 1
local frames = display.newImage("/Images/windowframes_gold.png", 155, 275)
display.newText(nameOptions)
local VN_pages = {
[1] = nil,
[2] = "\"Then, seeing as this is a simulation of\n a visual novel dating sim, I have no\n choice but to ask you...\"",
[3] = "\"My lady, would you go on a date with me?\nFrankenstein... butler of the fineest noble,\nCadis Etrama di Raizel?\"",
[4] = "duck",
[5] = "duck",
[6] = "duck",
}
local displayNovelText = display.newText("\"I see. So I\'m supposed to pretend I am\na character in a multi-chapter phone\napp that you\'ve been reading...\"", 165, 500, "Goudy Old Style Regular.ttf", 17)
function changePage()
print("dang it")
page = page + 1
displayNovelText.text = VN_pages[page]
display.newText(displayNovelText)
end
textRect:addEventListener("tap", changePage)
if (coins < 10) then
coinsDigits = 2
else
if (coins > 9) and (coins < 100) then
coinsDigits = 3
else
if (coins > 99) and (coins < 1000) then
coinsDigits = 4
else
if (coins > 999) and (coins < 10000) then
coinsDigits = 5
else
if (coins > 9999) and (coins < 100000) then
coinsDigits = 6
end
end
end
end
end
cooin = display.newSprite(coinimageSheet, sequenceData)
cooin.x = 25
cooin.y = 30
cooin:play()
coinText = display.newText("1", 57 + 4 * coinsDigits, 32, "VCR_OSD_MONO_1.001.ttf", 25)
coinText.text = coins
coinTimer = timer.performWithDelay(2000, cooin, 1)
end
function choiceMade( event ) --the scenes where all the choices are made
if (event.action == "clicked") then
local i = event.index
if (i == 1) then
Runtime:removeEventListener()
titleScreen()
else
if (i == 2) then
system.openURL( "https://www.paypal.com/us/home" )
else
if (i == 3) then
return
end
end
end
end
end -- end of choice scenes
function Outofcoins()
--native alert lack of zero coins
local alertMessage = "Uh oh, looks like you've run out of coins! To continue reading the story, would you like to buy or gameble for coins?"
native.showAlert( "Out of coins!", alertMessage, {"Gamble for coins", "Purchase coins", "Exit to Menu"}, choiceMade)
end
if (coins == 0) then -- conditional branch that alerts Outofcoins if no coins left
Outofcoins()
end
function sceneGambleStart()
function earntCoins()
numberEarnt = 0
local coinsGot = display.newImage("/Images/coins_gold.png", 155, 275)
coinsGot.alpha = 0
local function fadeOutCoinsEart()
transition.to(logo, {time = 2000, alpha = 0})
display.remove(coinsGot)
end
local transitionFade = transition.to(logo, {time = 2000, alpha = 1, onComplete = fadeOutCoinsEarnt})
timer.performWithDelay(2000, transitionFade, 1)
coinText.text = coins + numberEarnt
end
local function gamblerIntro()
nameOptions.text = "Gambler"
local bg = display.newImage("/Images/bg4.jpg", 155, 275)
textRect = display.newRect(155, 525, 325, 200)
textRect:setFillColor(.02, .02, .02)
textRect.alpha = .6
local frames = display.newImage("/Images/windowframes_gold.png", 155, 275)
display.newText(nameOptions)
if (gambleVisit == false) then
display.newText("\"Welcome to the coin gambling shop!\nHere's your chance to earn free coins\nwithout having to use the app store!", 165, 500, "Goudy Old Style Regular.ttf", 17)
--display.newText("\"You can play here once a day if you\'ve\nNO coins in your inventory! You are\ngiven three tries at any game each visit.", 165, 500, "Goudy Old Style Regular.ttf", 17)
--display.newText("\"So, then! What games will you play\nin our shop today? \n \n", 165, 500, "Goudy Old Style Regular.ttf", 17)
else
display.newText("\"Welcome back, player! You have\nthree tries left. So, what games\nwill you try your hand at?", 165, 500, "Goudy Old Style Regular.ttf", 17)
end
end
local function sceneDiceRoll()
--local show background graphics
--draw dice on screen, with function ready to shake/transition on screen when accelerometer
--transition hands up and drop dice animation
-- if dice = # chosen then give coins
end
local function sceneCardChoose()
-- a function that defines the mechanics of the card game.
--draw several options on the screen:
--3 cards, earn 5 coins.
--6 cards, earn 15 coins.
--9 cards, earn 30 coins.
--The player needs 5 coins to read another chapter, but by increasing card numbers,
--depending on the player's choice, show the images of the cards (with whatever numbers, always set)
-- on the screen and allow the player to choose a card. Make a shuffling animation.
-- lay all the cards on the screen, now with randomised positions defined by a number.
--the player may choose one. Event listener, if the number defined = card number, the card flips,
--shows its number, and the player wins the coins. defer to earntCoins.
--if the player chooses the wrong card, show him a "WRONG CARD" result, and ask if he would like another
--round or to exit to the main shop.
return
end
local function sceneGuessNumber()
--this game is not created, but is a dummy function that's shown here. It's included in the
--options to show that if this were a real game, that's what it would look like
return
end
end
The error message says all you need:
"mainl.lua:160: bad argument #1 to 'newText' (string expected, got
table)
if you go to line 160 of your code you'll find the following:
display.newText(displayNovelText)
a few lines above you do this:
local displayNovelText = display.newText("sometext")
Refer to the display.newText documentation to find out how to use this function correctly.
https://docs.coronalabs.com/api/library/display/newText.html
You'll see that display.newText() does not return a string, but a text-object.
It also does not take a text-object as input. That's what the error message is telling you.
To access the text of displayNovelText you have to write displayNovelText.text which you can stuff into display.newText() among other options...
Please make sure you always read the documentation of functions you use. You cannot be successful if you don't know what you are dealing with.

Resources