My cube in which I am updating to move 5 pixels every frame, will not move. I have checked my code and I cannot find the issue, send pizza. I wish to make the cube move right 5 pixels every frame.
JavaScript
x
43
43
1
#---- Main-----
2
import pygame, sys
3
from os import path
4
from settings import *
5
from sprites import *
6
7
class Game:
8
def __init__(self):
9
pygame.init()
10
pygame.display.set_caption(TITLE)
11
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
12
self.clock = pygame.time.Clock()
13
14
def update(self):
15
#self.dt = self.clock.tick(FPS) / 1000
16
self.new()
17
self.draw()
18
19
def draw(self):
20
self.sprites.update()
21
self.sprites.draw(self.screen)
22
pygame.display.flip()
23
self.clock.tick(30)
24
def events(self):
25
for event in pygame.event.get():
26
if event.type == pygame.QUIT:
27
self.quit()
28
29
def new(self):
30
self.screen.fill(DARKGREY)
31
self.sprites = pygame.sprite.Group()
32
player = Player(self)
33
34
def quit(self):
35
pygame.quit()
36
sys.exit()
37
38
39
while True:
40
g = Game()
41
g.events()
42
g.update()
43
separate file
JavaScript
1
15
15
1
#---sprites
2
import pygame
3
from settings import *
4
5
class Player(pygame.sprite.Sprite):
6
def __init__(self, game):
7
self.groups = game.sprites
8
pygame.sprite.Sprite.__init__(self, self.groups)
9
self.image = pygame.Surface((TILESIZE, TILESIZE))
10
self.image.fill(WHITE)
11
self.rect = self.image.get_rect()
12
13
def update(self):
14
self.rect.x += 5
15
separat file
JavaScript
1
18
18
1
WHITE = (255, 255, 255)
2
BLACK = (0, 0, 0)
3
DARKGREY = (40, 40, 40)
4
LIGHTGREY = (100, 100, 100)
5
GREEN = (0, 255, 0)
6
RED = (255, 0, 0)
7
YELLOW = (255, 255, 0)
8
9
WIDTH = 600
10
HEIGHT = 500
11
FPS = 60
12
TITLE = 'bruh zelda'
13
PLAYER_SPEED = 100
14
15
TILESIZE = 32
16
TILEWIDTH = WIDTH / TILESIZE
17
TILEHEIGHT = HEIGHT / TILESIZE
18
Advertisement
Answer
You have to create the instance of the Game
class before the application loop:
JavaScript
1
5
1
g = Game()
2
while True:
3
g.events()
4
g.update()
5
The Cube must be created in the constructor of the Game
class rather than in draw
:
JavaScript
1
14
14
1
class Game:
2
def __init__(self):
3
pygame.init()
4
pygame.display.set_caption(TITLE)
5
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
6
self.clock = pygame.time.Clock()
7
self.new()
8
9
def update(self):
10
#self.dt = self.clock.tick(FPS) / 1000
11
self.draw()
12
13
# [...]
14