Reputation: 68
import pygame
import random
import os
path = os.getcwd()
bg = pygame.transform.scale2x(pygame.image.load(f'{path}/space.jpg'))
images = [
pygame.image.load(f'{path}/R1.png'),
pygame.image.load(f'{path}/R2.png'),
pygame.image.load(f'{path}/R3.png'),
pygame.image.load(f'{path}/R4.png'),
pygame.image.load(f'{path}/R5.png'),
pygame.image.load(f'{path}/R6.png'),
pygame.image.load(f'{path}/R7.png'),
pygame.image.load(f'{path}/R8.png'),
pygame.image.load(f'{path}/R9.png')
]
win_width = 500
win_height = 600
win = pygame.display.set_mode((win_width, win_height))
class Spacemen(object):
def __init__(self, y, velocity):
self.y = y
self.velocity = 5
self.walk_count = 1
self.gravity = True
self.inverted_gravity = False
self.x = 1
def move(self):
if self.gravity and not self.inverted_gravity:
self.y += self.velocity
if not self.gravity and self.inverted_gravity:
self.y -= self.velocity
def draw(self, win):
if self.x == 10:
self.x = 1
if self.gravity and not self.inverted_gravity:
img = images[self.x]
elif not self.gravity and self.inverted_gravity:
img = pygame.transform.flip(images[self.x])
self.x += 1
win.blit(img, (256, self.y))
def game_window():
win.blit(bg, (0, 0))
Spacemen.draw(win)
win.update
def main():
clock = pygame.time.Clock()
man = Spacemen(256, 5)
running = True
while running:
pygame.time.delay(100)
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT or keys[pygame.K_ESCAPE]:
run = False
man.move()
game_window()
main()
When I run this it says that I am missing one positional error, missing "win" on line 54.
I do not know what is wrong because when I did something similar on a different project it worked perfectly. When I supply (win, win) another error pops up saying:
AttributeError: 'pygame.Surface' object has no attribute 'x'
Upvotes: 4
Views: 66
Reputation: 210878
draw
is a Method Object. You have to pass an Instance Object of the class Spaceman
to the function game_window
and to invoke the method draw
on the instance (man
).
def game_window(man):
win.blit(bg, (0, 0))
man.draw(win)
pygame.display.update()
Pass the instance man
of the class Spacemen
to the function game_window
:
def main():
# [...]
man = Spacemen(256, 5)
running = True
while running:
# [...]
game_window(man) # <-----
The the display is updated by either pygame.display.update()
or pygame.display.flip()
. win.update
makes no sense at all. A pygame.Surface
has no instance object update
and for a call statement, the parentheses are missing.
win.update
pygame.display.update()
Upvotes: 2