I'm currently trying to make my first game in Python. I just finished working on the movement but when my character jumps straight he just disappears. I am also trying to make my character shoot bullets but it wont show up. I don't know which part to change. I have not been able to figure this out on my own as I am still learning. Here is my code:
import pygame
from pygame.constants import K_SPACE
pygame.init()
win = pygame.display.set_mode((500, 480))
WinHeight = 480
WinWidth = 500
# player
char = pygame.image.load('standing.png')
# Player movement
walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'),
pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'),
pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'),
pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'),
pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]
# title
pygame.display.set_caption("First Game")
# background
bg = pygame.image.load('bg.jpg')
class Player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.left = False
self.right = False
self.walkCount = 0
self.isJump = False
self.jumpCount = 10
self.standing = True
def Draw(self, win):
if not (self.standing):
if self.walkCount + 1 >= 27:
self.walkCount = 0
if self.left:
win.blit(walkLeft[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
elif self.right:
win.blit(walkRight[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
else:
if self.right:
win.blit(walkRight[0], (self.x, self.y))
else:
win.blit(walkLeft[0], (self.x, self.y))
class Projectile(object):
def __init__(self, x, y, radius, color, facing):
self.x = x
self. y = y
self. radius = radius
self.color = color
self.facing = facing
self.vel = 8 * facing
def draw(self, win):
pygame.draw.circle(win, self.color, (self.x, self.y), self.radius )
# functions
def redrawGameWindow():
win.blit(bg,(0,0))
man.Draw(win)
for bullet in bullets:
bullet.draw(win)
pygame.display.update()
clock = pygame.time.Clock()
running = True
man = Player(50, 400, 64, 64)
bullets = []
while running:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
for bullet in bullets:
if bullet.x < 500 and bullet.x > 0:
bullet.x += bullet.vel
else:
bullets.pop(bullets.index(bullet))
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
if man.left:
facing = -1
else:
facing = 1
if len(bullets) > 5:
bullets.append(Projectile(round(man.x + man.width //2)), round(man.y + man.height), 30,(138,43,226), facing)
if keys[pygame.K_a] and man.x > man.vel:
man.x -= man.vel
man.left = True
man.right = False
man.standing = False
elif keys[pygame.K_d] and man.x < WinWidth - man.vel - man.width:
man.x += man.vel
man.right = True
man.left = False
man.standing = False
else:
man.walkCount = 0
if not (man.isJump):
if keys[pygame.K_w]:
man.isJump = 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) * 0.5 * neg
man.jumpCount -= 1
else:
man.isJump = False
man.jumpCount = 10
redrawGameWindow()