Im a newbie trying to make a progressive website. I want the users to go through 7 tests on each level and only if they complete 4/7 tests successfully will they be allowed to progress to the next level (webpage). How do i add this condition check of 4/7 successful completions to my button which will take the user to the next page?
Set the button's display to none in css
#buttonToNextPage{
display : none;
}
Using a variable in javascript to keep track of the number of tests your user has answered. The variable can start from 1 and go upto 4
var testAnswered = 1;
Every time the user answers a new test, you can increment the number by 1 by triggering a function. After incrementing, within the function u should check if the value has reached 4.If yes, you display the button using javascript. The function must look something like this:
function newTestAnswered(){
testAnswered = testAnswered + 1;
if(testAnswered == 4){
document.querySelector('#buttonToNextPage').style.display = block;
}
}
Hope this helped☺
I'm making a game with pygame, but I have a problem that button does not work as a toggle.
while True:
mousepos = pygame.mouse.get_pos()
mouseclk = pygame.mouse.get_pressed()
game.fill((0,0,0))
game.blit(cic, (0,0))
if mouseclk[0] and 1227 > mousepos[0] > 1120 and 485 > mousepos[1] > 413:
while True:
game.blit(pbut_rgm84, (1120,413))
pygame.mixer.music.load("sounds\se_beep.mp3")
pygame.mixer.music.play(0)
current_weaponinf = font.render(current_weapon[1], True, (255, 0, 0))
game.blit(current_weaponinf, (930,45))
else:
game.blit(but_rgm84, (1120,413))
This is my code for the button. As you know, If I click on that button, image of the button will be changed and sound will be played. But it will happen while i'm pressing on the button. So, I want to the button to be toggle type. Do you have any ideas on it?
EDIT 1: Sorry for the ramble. The answer is in the first part. The rest is just some notes on a more standard way to do what you asked for in PyGame. After all, the answer is not just for the OP, but everyone who visits this page.
EDIT 2: Notice also I had to change the code to lower down the FPS to 5 for this to work fine. The way you checked for a click meant that the mouse button was down for about 30 - 40 frames every click on my machine, hence resulting in 30 to 40 button toggles when only one was needed, so I had to lower down the FPS to 5 in the code. This is another reason why you might want to use the other method I described for event handling.
Answer:
I honestly appreciate the simple logic you used to check if the mouse click was in range of the object being pressed.
I could extend your code slightly to get the "toggle" type button you want (i.e, on a click, the image and sound changes. Click again, and the image and sound go back to normal).
However, I also would like to show you some better ways of dealing with mouse click events and click detection available in the Pygame module to improve your code. If you are interested and want to know what I mean, please read on after the code below. If you just wanna take the code and leave, I totally respect that (it's something I did a lot as well).
Here is your code modified to match your "toggle" button requirements (if I understood what you want to do right). Replace this with the part of your code you showed in your answer, but be aware that this may not work as I don't have your full code or the images / soundtracks you used :
toggle = False
pygame.mixer.music.load("sounds\se_beep.mp3")
pygame.mixer.music.play(-1)
pygame.mixer.music.pause()
clock = pygame.time.Clock()
while True:
mousepos = pygame.mouse.get_pos()
mouseclk = pygame.mouse.get_pressed()
game.fill((0, 0, 0))
game.blit(cic, (0, 0))
if mouseclk[0] and 1227 > mousepos[0] > 1120 and 485 > mousepos[1] > 413:
if toggle:
toggle = False
pygame.mixer.music.pause()
else:
toggle = True
pygame.mixer.music.unpause()
if toggle:
game.blit(pbut_rgm84, (1120, 413))
current_weaponinf = font.render(current_weapon[1], True, (255, 0, 0))
game.blit(current_weaponinf, (930,45))
else:
game.blit(but_rgm84, (1120,413))
clock.tick(5)
A Better Way: Handling Events
Handling mouse click events and in fact all other events is simple with what is called an event loop. Although there is a little bit more to it than this, for now we can say than an event loop checks for certain events triggered (such as key press, mouse click, quit game events) at every frame, and then triggers a callback function.
You already have one in your code! - At every loop in your while loop, you check if the mouse was clicked using mouseclk = pygame.mouse.get_pressed()
However this is incomplete and is not the standard way to implement event loops using the Pygame module. Here is how you do it (read the comment above each line to understand what it is doing) :
import pygame
from pygame.locals import *
...
# Main game loop - this is the while loop you have in your code. This is where frames are rendered on every new loop
while True:
#Get all event that have been triggered from the last frame
events = pygame.event.get()
# Now we go through each event separately
for event in events:
#Use an if statement to check what the event is. For example...
if event.type == MOUSEBUTTONDOWN: #If this event was a mouse button click
print("Clicked") #Do whatever function
elif event.type == QUIT: #If this event was quitting the game, i.e: pressing the X button on the game window
pygame.quit() #Close the game
#and so on. You can have as many elifs to check all the events you need
game.fill((0,0,0)) #... The rest of your code goes after the for loop (event loop)
So essentially in your main game loop (your while loop) you have inside it a for loop (the event loop), then after it back in the main while loop you have the rest of the code you need to run on every frame such as rendering images and animating sprites.
A Better Way: Rect Objects
You will find that to see if the mouse click position (x, y) was in range of the mouse button, you had to check if the mouse click's x value was in range of the X1 to X2 of the object you're clicking (top right and top left corners), and at the same time it also has to be in range Y1 to Y2 of the object (top right and bottom right corners), which effectively means the mouse was somewhere on the object when the mouse click event was triggered
For this, Pygame implements Rect objects. A rect can be explained as an imaginary rectangle with a width and height that you define when you create it ( I will show you how that is done in code below ). To be more realistic, it is a pygame object for storing rectangular co-ordinates (a range of all points within that rectangle).
Now the thing is that this rect object has methods like Rect.collidepoint, which will return True if the mouse click position is inside the rect. This is a more simplified, easier to understand way of detecting if a mouse click position is inside a rect or outside it.
So..
# Constructing a rect at position (0, 0) with width 100 and height 500
r = pygame.Rect(0, 0, 100, 50)
# Now lets assume that above there is code where a mouse click is detect and the position is stored in `mousepos`, where `mousepos[0]` is the X and `mousepos[1]` is the Y
# All we have to do now is tell pygame to check if this position is inside our Rect object *r* or not
if r.collidepoint(mousepos):
print("You clicked inside an imaginary box")
else:
print("You clicked outside my imaginary box")
# Simple!!!
Much easier and more readable! (especially after you remove the comments you will be dumbfounded by the simplicity of this (collidepoint means inside the rect).
In reality, this is not how you usually use rects, although it gives you the basic idea of what they are. Rects are used in a more object-oriented method where the button is a child class of the Pygame.sprite.Sprite class. Then you can assign a rect object as its self.image.rect property and use it from there. NOTE: this is not very detailed :D .
Also, this is not the only use of rects. You can check if one rect is inside another. You can check if a group of objects is inside a rect. You can even with only one line of code check whether one object of a group is inside a certain rect, and if so, remove it!
To understand more about how to code pygame from start to finish (as the extra part of this answer talking about events and rects is very incomplete and only there as a starting point), I would suggest this excellent yet very simple and easy to understand book. And all you need is a basic understanding of python syntax.
So in my game I spawn a grapple hook when I touch the screen, connecting player and ceiling. The problem is that the touchDown() of my inputAdapter only seems to activate the first time I touch, nothing happens after when I touch again (meaning: no other "grapple hooks" get created, just the one). touchUp() or every other input method still work.
Here are all the classes: Rope - GamePlay - InputManager and also just in case MainMenu
EDIT
So I was being a moron and in the overlapsOnX() method forgot to add and substract the width of the "cloud" so unless I got very lucky (or spawned in the right place) the rope wouldnt get created.
Here's how the method should look like
private boolean overlapsOnX(Body player, Body cloud){ //check if the player is currently in the same X position than a cloud
return player.getPosition().x >= cloud.getPosition().x - (20 / PPM) && player.getPosition().x <= cloud.getPosition().x + (20 / PPM);
}
TouchDown returns a boolean to indicate if the input has been processed or not. You are returning false, but you probably mean to return true. This would indicate the input has finished being handled.
I have a similar animation in Flash with a replay button at the bottom. Once the animation completes, it does not loop. Instead if you click replay, the animation starts again.
I want a textbox which will display the amount of times the button was pressed.
My existing code is currently replaying the animation. It's incrementing the count to 1 (from 0) in trace but not getting set within the textbox. Additionally, if I click the button again, trace displays 1 again -- no increment occurred (1 to 2, 2 to 3, etc).
If I comment out gotoAndPlay(1), the incrementing works just fine and is displayed in the textbox -- but the animation does not play again.
What am I doing wrong?
import flash.events.MouseEvent;
var hitcount:Number = 0;
textCounter.text = String(hitcount);
function incCounter(event:MouseEvent):void
{
hitcount++;
textCounter.text = String(hitcount);
trace(hitcount);
gotoAndPlay(1);
}
replay_btn.addEventListener(MouseEvent.CLICK, incCounter);
The problem in your code is that you are creating and initializing every time your counter, so sure you will get every replay the value 1.
So to avoid that, you can just verify if you have already created the hitcount var otherwise create it :
if(!hitcount){ // hitcount is null, so create it
var hitcount:Number = 0;
}
As a good practice, you can also remove the MouseEvent.CLICK on your button if you don't need it. So you get something like this :
if(!hitcount){
var hitcount:Number = 0;
}
count.text = hitcount.toString();
function incCounter(event:MouseEvent):void
{
event.target.removeEventListener(MouseEvent.CLICK, incCounter);
hitcount++;
count.text = hitcount.toString();
gotoAndPlay(1);
}
replay_btn.addEventListener(MouseEvent.CLICK, incCounter);
Hope that can help.
If your flash movie is longer than one frame then the layer which you are placing your actions on the stage needs to extend that amount of frames as well. Otherwise the code is no longer there after frame 1. Also when you come back to frame 1 you are attaching yet another mouse event each time. Start your code with yourButton.removeEventListener to ensure you are only ever attaching one event to it. And every time the movie ends and goes back to frame 1 your counter is reset to 1.
I'm a bit new to Flash. As I was developing my movie, I introduced 3 layers of sequential animation that I decided I wanted to loop X times. So I copied the frames, created a new Movie Clip symbol, pasted the frames into it, then deleted the original on the stage and dragged this new clip as an instance (my_animation) in its place. All good so far.
However, when I play the entire movie, my_animation looped indefinitely, so I added a stop() action into that movie clip. That worked - plays once. I then added the following as an action on the very last keyframe of the main scene to try and get my_animation to loop X times:
var i = 1;
for (i = 1;i < 5;i++)
{
my_animation.play();
}
stop();
However, my_animation still only plays once. It does not appear to loop as I expect. I also tried replacing my_anmiation.play() with gotoAndPlay(117) where 117 is the frame containing the movie clip, but still the same.
Any help appreciated.
Thanks
UPDATE: I added a trace(i); statement within the loop. In the Output tab, I see:
1
2
3
4
…etc. SO - my_animation is not firing within the loop.
if you call my_animation.play(); with in "for loop" like that, it just calls 5 time consecutively and it just starts animation .
if you call my_animation.play(); during animation of my_animation nothing
happens. it works if it has stopped...
. so you need to listen add callback for to understand that animation finished and you can start that animation again..
var animatedCount:int = 0;
my_animation.addFrameScript(my_animation.totalFrames-1, onMyAnimationEnds);
my_Animation.play();
function onMyAnimationEnds():void{
animatedCount++;
if(animatedCount < 5)
{
my_Animation.play();
}
}
this will animate my_animation for 5 time...