Python Forum
My code does not work as expected (pygame)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My code does not work as expected (pygame)
#1
Hello,
I am trying to make a game in pygame and after i optimised my code it stopped working.
It is creating a window but it is black .

Thank you for helping.

Here is my code:
import pygame
import sys
from pygame.constants import K_RIGHT

pygame.init()

screen_width = 800
screen_hight = 600

game_window = pygame.display.set_mode((screen_width, screen_hight))
pygame.display.set_caption("Window")

walkleft = (pygame.image.load("D:\python project\pygametest\idle.png"))
walkright = (pygame.image.load("D:\python project\pygametest\idleflip.png"))
idle = (pygame.image.load("D:\python project\pygametest\eidle.png"))
bg = (pygame.image.load("D:\python project\pygametest\ibg.png"))

clock = pygame.time.Clock()

class player (object):
    def __init__(self, x, y, screen_width, screen_hight):
        self.x=x
        self.y=y
        self.screen_width=screen_width
        self.screen_hight=screen_hight
        self.vel=5
        self.is_jump=False
        self.jumpcount=10
        self.left=False
        self.right=False
        self.walkcount=0

    def draw(self,game_window):
        if self.walkcount +1 >= 27:
            self.walkcount = 0
        
        if self.left:
            game_window.blit(walkleft,(self.x,self.y))
            self.walkcount += 1
        elif self.right:
            self.game_window.blit(walkright,(self.x,self.y))
            self.walkcount += 1
        else:
            game_window.blit(idle, (self.x,self.y))

def redrawwindow():
    global walkcount
    man.draw
    pygame.display.update()
    
#mainloop

man=player(300 ,400,64,64)

game_running = True

while game_running:
    clock.tick(27)
    
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and man.x > man.vel:
            man.x -= man.vel
            man.right = False
            man.left = True
    elif keys[pygame.K_RIGHT] and man.x < man.screen_width - man.vel:
            man.x += man.vel
            man.right = True
            man.left = False
    else:
        man.right = False
        man.left = False
        man.walkcount = 0

    if not (man.is_jump):
        if keys[pygame.K_SPACE]:
            man.is_jump = True
            man.right = False
            man.left = False
            man.walkcount = 0

    else:
        if man.jumpcount >= -10:
            neg = 1
            if man.jumpcount < 0:
                neg = -1
            man.y -=(man.jumpcount ** 2) / 4 * neg
            man.jumpcount -= 1

        else:
            man.is_jump= False
            man.jumpcount = 10

    redrawwindow()





pygame.quit()
sys.exit()
Reply


Messages In This Thread
My code does not work as expected (pygame) - by rohes_kaugummi - Aug-25-2021, 08:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  my simple code wont work! help! simon12323121 2 2,043 Sep-05-2021, 09:06 AM
Last Post: naughtyCat
  I wrote a code but it does not work out as expected chinitayao226 2 1,892 Sep-23-2020, 09:14 PM
Last Post: SnowwyWolf
  A program for backing up documents (doesnt work(code is inside)) Richard_SS 4 3,459 Jun-05-2019, 03:47 PM
Last Post: heiner55
  I Can't Get This Sorting Function In This LinkedList Code To Work JayJayOi 10 7,966 Jan-11-2018, 01:14 AM
Last Post: JayJayOi
  Need help with getting this voting eligiblity code to work Beastly 1 2,540 Aug-16-2017, 03:46 PM
Last Post: ichabod801
  Why can't this code work. sexualpanda 2 3,440 Feb-06-2017, 04:03 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020