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 9204363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:41:43+00:00 2026-06-17T23:41:43+00:00

I am making a 2-player game which is supposed to look like: In the

  • 0

I am making a 2-player game which is supposed to look like:
Game image
In the game, the shooters(green and blue, controlled by players) can shoot bullets at each other.
If the bullet
1. collides with the wall(grey), it gets destroyed.
2. hits the shooter, it loses health(shooter).
The game is (supposed to be) turn-based and ends when a player reaches 0 health.

My Problem(s)
1. The barrel of my shooters is not updating/rotating.
2. Is there a better way to detect if a(ny) key is pressed.
* Shooter,Bullet,Wall are classes

My code
(comment if any function that would be useful in answering is not mentioned)

import math,random,pygame
def event_handle(event,turn):
    if turn == 1:
        c_s = p1
    elif turn == 2:
        c_s = p2
    else:
        return None
    if event.type == pygame.KEYDOWN:
        key = pygame.key.get_pressed()
        # next_pos
        if key[pygame.K_q]:
            c_s.next_x -= 1
        if key[pygame.K_e]:
            c_s.next_x += 1
        # angle
        if key[pygame.K_w]:
            c_s.angle += radians(1)
        if key[pygame.K_s]:
            c_s.angle -= radians(1)
        # power (speed)
        if key[pygame.K_d]:
            c_s.speed += 0.1
        if key[pygame.K_a]:
            c_s.speed -= 0.1

def draw_all(bullist,shooters,wall,surface):
    # draw shooters
    for shooter in shooters:
        shooter.move()
        shooter.draw(surface)
    # draw bullets
    for bullet in bullist:
        bullet.gravity()
        bullet.move()
        bullet.collides(shooters,wall,bullist)
        bullet.out(surface,bullist)
        bullet.draw(surface)
    # wall
    wall.update()
    wall.draw(surface)
    pygame.draw.aaline(surface,(255,255,255),(0,400),(640,400))

def angle(A,B,BC,theta):

    C = [0,0]
    alpha = math.atan2(A[1]-B[1] , A[0] -B[0] ) - theta
    C[0] = int(round(B[0]  + BC * math.cos(alpha),0))
    C[1] = int(round(B[1]  + BC * math.sin(alpha),0))
    return C

class Shooter:
    def __init__(self,pos,size,color,xmax,xmin):
        self.pos = pos
        self.size = size
        self.rect = pygame.Rect(pos,size)
        self.health = 100
        self.color = color
        self.angle = 0
        self.speed = 0
        self.max = xmax
        self.min = xmin
        self.next_x = pos[0]

        self.color2 = []
        for i in color:
            i = i - 100
            if i < 0:
                i = 0
            self.color2.append(i)

    def draw(self,surface):
        global C
        pygame.draw.rect(surface,self.color,self.rect)

        c = angle(self.rect.midleft,self.rect.center,
                  20,radians(self.angle))
        if c != C and c != [95,392]:
            print c
            C = c
        pygame.draw.line(surface,self.color2,self.rect.center,c,3)


## the other funcs or classes not needed

# globals
turn = 1
C = []
# pygame
(width, height) = (640, 480)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Shooter')
clock = pygame.time.Clock()
# game actors
shooters = []
bullets = []
p1 = Shooter((400,400),(30,-15),(255,0,0),0,0)
p2 = Shooter((100,400),(30,-15),(0,255,0),0,0)
shooters.extend([p1, p2])
wall = Wall(100)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
            break
        else: event_handle(event,turn)
    if not running:
        break
    screen.fill((0,0,0))
    # Game draw logic + Game logic

    draw_all(bullets,shooters,wall,screen)
    pygame.display.flip()
    clock.tick(40)

What am I doing wrong?

  • 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-17T23:41:45+00:00Added an answer on June 17, 2026 at 11:41 pm

    Your code actually works, but there are some problems. The barrels actually rotate, but only a very small amount on every key press.

    Try and change your event_handle function to this:

    def event_handle(turn):
        if turn == 1:
            c_s = p1
        elif turn == 2:
            c_s = p2
        else:
            return None
        key = pygame.key.get_pressed()
        # next_pos
        if key[pygame.K_q]:
            c_s.next_x -= 1
        if key[pygame.K_e]:
            c_s.next_x += 1
        # angle
        if key[pygame.K_w]:
            c_s.angle += radians(10)
        if key[pygame.K_s]:
            c_s.angle -= radians(10)
        # power (speed)
        if key[pygame.K_d]:
            c_s.speed += 0.1
        if key[pygame.K_a]:
            c_s.speed -= 0.1
    

    Since you are not interessed in the event type at all at this point, I removed the event parameter and the if event.type == pygame.KEYDOWN: check. This way, you can keep your keys pressed instead of being forced to hit the keys multiple times to rotate the barrels.

    I also increased the value the barrels rotate from radians(1) to radians(10). Otherwise, the change is too small to be visible (in a reasonable amount of time).

    Also, you have to adjust your main loop to

    ...
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
            break
    if not running:
        break
    event_handle(turn)
    ...
    

    so event_handle is called every iteration of the main loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a online game (with hopefully thousands of players each hour) which uses
I am making a card game where the player can touch and drag around
I'm making a 2D map-based game. The player controls one character while several AI-controlled
I am making a game in which certain events require every player to input
I am making a snake game which requires the player to press the WASD
I'm making a programming game where the player can program their allies' behavior. The
I am making a game which uses collision detection with the mouse. The player
I am making a two player game applet. Both players need to control their
I'm making a game and I want to check collision between player and block,
I'm making a game. Now if my player goes to another level the music

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.