Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7964355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:52:04+00:00 2026-06-04T05:52:04+00:00

Hello again Stack Overflow. you probably remember me from my unit spawning problem in

  • 0

Hello again Stack Overflow. you probably remember me from my unit spawning problem in my pygame program, “Table Wars.” I decided to change the scope of my game to a real-time strategy rather than a turn-based game. I want the game to play along the lines of top Flash game: “Age of War.” Almost everything works in the game: spawning units, the HUD for the game, and even base health. Unfortunately, I can’t seem to figure out how to implement the ability for units to attack enemies or the enemy base. Here is the concepts going on for the units themselves:

  • The unit spawns on a keypress around the team’s base: K_1 spawns a sprite from the Red_Infantry class
  • The unit, when spawned, is added to a Group class. There are two Groups, one for each team.
  • The unit moves via a move_ip call within a def update until it reaches a point close to the enemy base, where it stops.

Here is how I want combat to go for the different units:

  • The unit stops whenever it spots an enemy within it’s attack range. Different units have different attack ranges
  • The unit then attacks in one-second intervals.
  • If the unit sucessfully reduces the enemy unit’s health to 0, the enemy dies, and the other may continue
  • This cycle repeats until the unit reaches the enemy base, where it will then attack the enemy base on one-second intervals. One of the units will be able to deal triple the normal damage to the base.

Here is a sample of my code, showing the Red_Infantry class:

class Red_Infantry(pygame.sprite.Sprite):
def __init__(self, screen):
    pygame.sprite.Sprite.__init__(self)
    self.image, self.rect = load_image('Soldier_red.png', -1)
    self.rect.move_ip(random.randint(75, 100), random.randint(275, 325))
    self.selected = 0
    self.area = screen.get_rect()
    self.health = 100 #Soldiers are have mediocre toughness.
    self.attack_damage = 25 #The amount of damage it deals
    self.range = 20 #The attack range of the unit.
    self.update()
def update(self):
    self.rect.move_ip(1, 0)
    if self.rect.right >= 725: #This position is close to the enemy base...
        self.rect.right = 725 #...where it will then stop
    if self.health <= 0:  
        self.kill() #a simple code that kills the sprite if his health reaches 0

The main loop only contains the ability to spawn each of the units.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:52:06+00:00Added an answer on June 4, 2026 at 5:52 am

    Here’s a starting point

    class RedInfantry(pygame.sprite.Sprite):
        def __init__(self):
            self.screen = pygame.display.get_surface()
            self.image, self.rect = load_image('Soldier_red.png', -1)    
            self.rect.move_ip(random.randint(75, 100), random.randint(275, 325))
            self.target = None
            self.range = 20
            self.attack_cooldown = 200
            self.attack_last = 0
    
        def move_toward(self, target):
            # move toward players click destination, if one is set.
            # else toward my attack target
    
        def update(self):
            # move...
            self.find_target()
            self.move_toward(self.target)
            self.attack()
            # check HP
            if self.health <= 0:
                self.kill()
    
        def find_target(self):
            """finds new targets in range:
            for speed: only call this once every 200ms."""
            if self.target is not None: return
            for enemy in B.sprites():
                if distance(self.rect.center, enemy.rect.center) <= self.range:
                    self.target = enemy
                    return
            # else no targets in range
            self.target = None  
    
    
        def attack(self):
            """attack, if able.
            target exists? still alive? gun cooldown good?"""
            if self.target is None: return
            if self.target.health <= 0: return
            if not self.cooldown_ready(): return
    
            # all good, fire. could verify still in range. 
            self.target.damage(self.attack_damage)
    
        def cooldown_ready(self):
            # gun ready to fire? has cooldown in MS elapsed.
            now = pygame.time.get_ticks()
            if now - self.attack_last >= self.attack_cooldown:
                self.attack_last = now
                return True
            return False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello again ladies and gents! OK, following on from my other question on ASP.NET
Hello I've runned in to a problem again that I can't solve on my
Hello again stackoverflow... Once again I have a troublesome problem. I have a page
I have a compiling problem. I followed tutorial about Hello world program for Android
Hello again Stackoverflow people! Assume I have these words: smartphones, smartphone I want to
Hello I have strange problem. I configured spring + hibernate. I`m using annotation configuration
Hello I am new to the Objective C and I have a problem. I
Hello and Happy New Year all! While thinking about new project I decided to
Hello I have this query... if (isset($_REQUEST['deletePost'])) { $q = $dbc -> prepare(DELETE FROM
Hello again great knowledge masters of stackoverflow, once again the small coder apprentice tabaluga

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.