Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

How do I loop and move a transparent gif on second play?

Edrol97

Bronze Coder
Hi all. This is the link to a new website page of mine for a whack a mole game. I want the hole background to have worms moving along it and for it not to effect the game's location on the page. How do I do this?
 
Hey Edrol97!

That sounds like a fun effect! To make a moving worm background without messing with your game layout, you could use a CSS animation on a background div. Here’s a simple way to get started:

  1. Create a div that’ll be the "background" for your worms.
  2. Use CSS animations to make the worms move across that div.
Here’s a rough idea:

Code:
<!-- HTML -->
<div class="background">
</div><div class="game">
 <!-- Your game code here --> 
</div>

<!-- CSS -->
<style> 
 .background {    position: fixed;    top: 0;    left: 0;    width: 100vw;    height: 100vh;    z-index: -1;    overflow: hidden;  }   
 .worm {    position: absolute;    width: 50px;    height: 10px;    background-color: brown;    border-radius: 5px;    animation: moveWorms 10s linear infinite;  }  
/* Worm animation */  
@keyframes moveWorms {    0% { transform: translateX(-100%); }    100% { transform: translateX(100%); }  }</style>

You could add more &lt;div class="worm"&gt; elements within the .background div and randomize their start positions to make it look more dynamic.

Let me know if you want help tweaking it!
 
It is also possible to use multiple background-images for an element, such as a div.

CSS:
div{
    background-image:
        url('img/worm.gif'),
        url('img/worm.gif'),
        url('img/worm.gif'),
        url('img/worm.gif');
    background-position:
        left top,
        center top,
        right top,
        center;
    background-repeat: no-repeat; /* or repeat for all if needed */
}

Adjust all as necessary for your needs.
 
It is also possible to use multiple background-images for an element, such as a div.

CSS:
div{
    background-image:
        url('img/worm.gif'),
        url('img/worm.gif'),
        url('img/worm.gif'),
        url('img/worm.gif');
    background-position:
        left top,
        center top,
        right top,
        center;
    background-repeat: no-repeat; /* or repeat for all if needed */
}

Adjust all as necessary for your needs.
Managed to do it as so:

link
 
Actually, looks like you can solve the scrolling by putting box-sizing: border-box; on .game-container.

I use box-sizing: border-box; on all pages as part of my reset.css like this:

html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}

If you aren't doing this now, adding it will impact quite a bit - either subtly or greatly.

Read up here:
 

New Threads

Buy us a coffee!

Back
Top Bottom