I have problem with function. I starting learn pygame with video course, and this is my first pygame code. My .py code:
JavaScript
x
95
95
1
import pygame
2
3
pygame.init()
4
5
win = pygame.display.set_mode((1280, 720))
6
7
pygame.display.set_caption("text.")
8
9
walkaniml = pygame.image.load('left1.png')
10
walkanimr = pygame.image.load('right1.png')
11
stickmanStand = pygame.image.load('stickman.png')
12
13
clock = pygame.time.Clock()
14
15
x = 250
16
y = 400
17
widht = 271
18
height = 293
19
speed = 5
20
21
jump = False
22
jumplov = 10
23
24
left = False
25
right = False
26
animcount = 0
27
28
def drawcube():
29
30
global animcount
31
32
win.fill((255, 218, 185))
33
34
if animcount + 1 >= 24:
35
animcount = 0
36
37
if left:
38
win.blit(walkaniml(animcount // 1, (x, y)))
39
40
elif right:
41
win.blit(walkanimr(animcount // 1, (x, y)))
42
43
else:
44
win.blit(stickmanStand, (x, y))
45
46
pygame.display.update()
47
48
run = True
49
50
while run:
51
clock.tick(24)
52
53
for event in pygame.event.get():
54
if event.type == pygame.QUIT:
55
run = False
56
57
keys = pygame.key.get_pressed()
58
59
if keys[pygame.K_LEFT] and x > 1:
60
x -= speed
61
left = True
62
right = False
63
64
elif keys[pygame.K_RIGHT] and x < 1280 - widht - 1:
65
x += speed
66
left = False
67
right = True
68
69
else:
70
left = False
71
right = False
72
animcount = 0
73
74
if not(jump):
75
76
if keys[pygame.K_DOWN] and y < 720 - height - 1:
77
y += speed
78
79
if keys[pygame.K_SPACE]:
80
jump = True
81
else:
82
83
if jumplov >= -10:
84
if jumplov < 0:
85
y += (jumplov ** 2) / 3
86
87
else:
88
y -= (jumplov ** 2) / 3
89
jumplov -= 1
90
91
else:
92
93
jump = False
94
jumplov = 10
95
enter image description here drawcube()
I wanna do a mini-game with stickman, and i found a very big problem, when I start game in cmd and I’m going to web to found decision, but.. I don’t find him. Please guys, I realy need help((
Advertisement
Answer
walkaniml
and walkanimr
are pygame.Surface
objects. You can’t call an object:
win.blit(walkaniml(animcount // 1, (x, y)))
JavaScript
1
2
1
win.blit(walkaniml, (x, y))
2
If walkaniml
and walkanimr
are a list of Surfaces, you can get an element from the lists by it’s index with subscription (even if the list contains only 1 element):
JavaScript
1
3
1
walkaniml = [ pygame.image.load('left1.png') ]
2
walkanimr = [ pygame.image.load('right1.png') ]
3
JavaScript
1
10
10
1
if left:
2
if animcount >= len(walkaniml):
3
animcount = 0
4
win.blit(walkaniml[animcount], (x, y))
5
6
elif right:
7
if animcount >= len(walkanimr):
8
animcount = 0
9
win.blit(walkanimr[animcount], (x, y))
10