'Tech With Tim Pygame Tutorial
So I tried Tech With Tim's pygame tutorial series and I tried doing the animation code but it says "List Index Out of Range".
Here's my code:
from operator import neg
import pygame,sys
from pygame.locals import *
from pygame import image as img
pygame.init()
res = pygame.display.Info()
win = pygame.display.set_mode((res.current_w, res.current_h))
pygame.display.set_caption("Vixens Adventure PRE-ALPHA 0.01")
clock = pygame.time.Clock()
player_right = [img.load("B:/Vixens Adventure/Assets/player/right1.png"), img.load("B:/Vixens Adventure/Assets/player/right2.png"), img.load("B:/Vixens Adventure/Assets/player/right3.png"), img.load("B:/Vixens Adventure/Assets/player/right4.png"), img.load("B:/Vixens Adventure/Assets/player/right5.png")]
player_left = [img.load("B:/Vixens Adventure/Assets/player/left1.png"), img.load("B:/Vixens Adventure/Assets/player/left2.png"), img.load("B:/Vixens Adventure/Assets/player/left3.png"), img.load("B:/Vixens Adventure/Assets/player/left4.png"), img.load("B:/Vixens Adventure/Assets/player/left5.png")]
bg = img.load("B:/Vixens Adventure/Assets/bgs/bg1.png")
char = img.load("B:/Vixens Adventure/Assets/player/right1.png")
x = 50
y = 650
width = 32
height = 64
vel = 3
svel = 5
left = False
right = False
walkCount = 0
isJump = False
jumpCount = 10
def redrawGameWindow():
global walkCount
win.blit(bg, (0,0))
if walkCount + 1 >= 60:
walkCount = 0
if left:
win.blit(player_left[walkCount//3], (x, y))
walkCount += 1
elif right:
win.blit(player_right[walkCount//3], (x, y))
walkCount += 1
else:
win.blit(char, (x, y))
pygame.display.update()
clock.tick(27)
run = True
while run:
print(clock)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and x > vel:
x -= vel
left = True
right = False
if keys[pygame.K_LSHIFT]:
x -= svel
left = True
right = False
elif keys[pygame.K_d] and x < res.current_w - width - vel:
x += vel
left = False
right = True
if keys[pygame.K_LSHIFT]:
x += svel
left = False
right = True
else:
right = False
left = False
walkCount = 0
if not(isJump):
if keys[pygame.K_SPACE]:
isJump = True
right = False
left = False
walkCount = 0
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 1
else:
isJump = False
jumpCount = 10
redrawGameWindow()
if keys[pygame.K_ESCAPE]:
run = False
pygame.quit()
sys.exit()
pygame.quit()
sys.exit()
I tried switching up the frames per second to 27 but it still did not work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
