Aug-25-2021, 08:50 PM
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:
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()