0

I'm making key inputs to be able to move my character in a game, however when I run the program and press the key the character doesn't move, the animation works fine though. When I release the key and press it again the character moves like it should.

import pygame

def Screen1(pygame, win):
    import SpriteSheet


    Background= pygame.image.load("Graphics/ForestBackground.png") #Fond
    Ground=pygame.image.load("Graphics/ForestGround.png") #Sol

    pos_x = 0
    step_x = 10
    pos_y = 615
    step_y = 35

    direction = "RIGHT"

    ############################ Import du Sprite Sheet ##################################
    sprite_sheet_img=pygame.image.load("Graphics/Vita.png").convert_alpha()
    SpriteSheet=SpriteSheet.SpriteSheet(sprite_sheet_img)
    Black=(0,0,0)

    ########################Liste des Frames d'Animation##################################
    #IDLE #RUN #GAMEOVER
    animation_list=[]
    animation_steps=[4,6,3,4,7]
    animation_action=0

    ################ Animation #####################
    last_maj=pygame.time.get_ticks() #ms since pygame.init got initialized
    animation_cooldown= 240 #ms
    frame=0 #Animation commence au frame 0
    StepCounter=0

    win_x=850
    win_y=723


    for animation in animation_steps: #Choisir le type d'animation à faire
        TempImgList=[] #Liste temporaire
        for a in range(animation): #variable "a" n'importe pas
            TempImgList.append(SpriteSheet.get_image(StepCounter,24,24,3,Black))
            StepCounter=StepCounter + 1
        animation_list.append(TempImgList)


    run=True

    screenmode="Screen1"

    while run==True:

        if screenmode=="Screen1":

            current_time=pygame.time.get_ticks()
            if current_time-last_maj>=animation_cooldown:
                frame=frame+1
                last_maj=current_time #reset cooldown
                if frame >= len(animation_list[animation_action]): #reset l'animation
                    frame=0

            win.blit(Background,(0,0))
            win.blit(Ground,(0,-50))





            if direction == "RIGHT":
                win.blit(animation_list[animation_action][frame],(pos_x,pos_y))

            if direction == "LEFT":
                win.blit(pygame.transform.flip(animation_list[animation_action][frame],True,False).convert_alpha(),(pos_x,pos_y))


            for event in pygame.event.get():
                if event.type==pygame.KEYDOWN:
                    if event.key==pygame.K_ESCAPE:
                        return "Pause"

                if event.type==pygame.QUIT:
                        return "Quit"

                if event.type==pygame.KEYDOWN:
                    if event.key==pygame.K_RIGHT:
                        pygame.key.set_repeat(60)
                        pos_x=pos_x + step_x
                        direction = "RIGHT"
                        if pos_x > win_x:
                            pos_x=win_x

                        animation_action=4


                    if event.key==pygame.K_LEFT:
                        pygame.key.set_repeat(60)
                        pos_x=pos_x - step_x
                        direction = "LEFT"

                        if pos_x < 0:
                            pos_x=0

                        animation_action=4


            pygame.display.update()

pygame.quit()

Here's the main code to be able to run the program

import pygame
import Screen1
import Pause
import time

win_x=928
win_y=723

pygame.init()



win = pygame.display.set_mode((win_x,win_y))

pygame.display.set_caption("Game Name change later MAIN MENU")
clock =pygame.time.Clock()  #FPS Clock

Start=pygame.image.load("Graphics/Buttons/Start.png").convert_alpha()
StartHover=pygame.image.load("Graphics/Buttons/StartHover.png").convert_alpha()
Quit=pygame.image.load("Graphics/QUIT.png").convert_alpha()
Quit2=pygame.image.load("Graphics/QUIT.png").convert_alpha()
Info=pygame.image.load("Graphics/Info.png").convert_alpha()
Background= pygame.image.load("Graphics/ForestBackground.png") #Fond
######################
class Button():
    def __init__(self,x, y, image,imageOn, scale):
        width=image.get_width()
        height=image.get_height()
        self.image=pygame.transform.scale(image,(int(width * scale),int( height*scale)))
        self.imageOn=pygame.transform.scale(imageOn,(int(width * scale),int( height*scale)))
        self.rect=self.image.get_rect()
        self.rect.topleft=(x,y)
        self.clicked=False

    def draw(self):
        action=False
        position=pygame.mouse.get_pos()
        if self.rect.collidepoint(position):
            win.blit(self.imageOn, self.imageOn.get_rect(center=self.rect.center))

            if pygame.mouse.get_pressed()[0]==True and self.clicked==False:
                self.clicked=True
                action=True

            if pygame.mouse.get_pressed()[0]==False:
                self.clicked=False
        else:
            win.blit(self.image,(self.rect.x, self.rect.y))

        #dessine le bouton sur l'ecran
        return action





######################
start_button=Button(265,200,Start,StartHover,3)
quit_button=Button(352,440,Quit,Quit,1.7)
quit2_button=Button(352,550,Quit,Quit,1.7)
info_button=Button(450,620,Info,Info,1.7)
######################
run=True

info=False

screenmode = "Menu"

while run==True:
#Boucle pour laisser la fenêtre ouvert + faire les évenements du jeu

    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            run=False #Ferme le jeu



    if screenmode=="Menu":

        win.fill((202,228,241))
        win.blit(Background,(0,0))

        if start_button.draw() ==True:
            screenmode="Screen1"

        if quit_button.draw()==True:
            run=False

        if info_button.draw()==True:
            screenmode="Info"


    if screenmode=="Info":

        win.fill((202,228,241))
        win.blit(Background,(0,0))


        if quit2_button.draw()==True:
            screenmode="Menu"

    if screenmode=="Screen1":
        print("In screen 1")
        screenmode=Screen1.Screen1(pygame, win)


    if screenmode=="Pause":

        screenmode=Pause.Pause(pygame, win)
        if screenmode == "Quit":
            run=False #Ferme le jeu

    if screenmode == "Quit":
        run=False #Ferme le jeu

    pygame.display.update()
    clock.tick(60)
#####################
pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Axley
  • 1

0 Answers0