B
B
Bars10112021-12-02 03:33:35
pygame
Bars1011, 2021-12-02 03:33:35

How to implement control for android on pygame?

I'm studying the pygame library, I decided to write a game for the phone, similar to space invaders, and I can't figure out how to implement control for android. That is, touch, because there is no keyboard like on a PC. It is necessary that the character can be moved around the screen by moving your finger to the left and right. I've been worrying about this for several days now, looking for various information, trying a bunch of different options. And to be honest, he himself is already confused in his writing. Help please someone. Below is the code for all game files. Controls are responsible for management, for the location and initialization of the hero player.
main.py

import pygame, controls
import sys
from hero import Hero

def run():
  
  pygame.init()
  screen = pygame.display.set_mode((700, 800))
  pygame.display.set_caption("Space invaders")
  clock = pygame.time.Clock()
  bg = pygame.image.load("images/bg.jpg")
  hero = Hero(screen)
  surfrect = screen.get_rect
  mouseClicked = False
  while True:
       controls.events(hero)
   	  hero.update_hero()
       screen.blit(bg, (0, 0))
       hero.output()
   	  pygame.display.flip()


run()

hero.py
import pygame

class Hero():
    
    def __init__(self, screen):
      self.screen = screen
      self.image = pygame.image.load("images/idle.png")
      self.rect = self.image.get_rect()
      self.screen_rect = screen.get_rect()
      self.rect.x = 278
      self.rect.y = 450
      self.xnew = self.rect.x
      self.ynew = self.rect.y
      self.per = False
    
    def output(self):
      
      self.screen.blit(self.image, self.rect)

controls.py
import pygame, sys

def events(hero):
        mouseClicked = False
        for event in pygame.event.get():
          if event.type == pygame.QUIT:
            sis.exit()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Chetchasov, 2022-03-05
@TalismanChet

Control on android works almost the same as on PC, only instead of MOUSBUTTONDOWN there is FINGERDOWN, and instead of MOUSEBUTTONUP - FINGERUP. If you need information like "how to swipe in pygame on Android" , then reformulate the question. If it was helpful, please mark my answer as correct. Thanks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question