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()
当我运行代码时,它显示我缺少一个位置参数,在第54行缺少"win"。我不知道问题出在哪里,因为当我在另一个项目中尝试类似的方法时,它完美地工作了。当我提供(win, win)时,会出现另一个错误。
AttributeError: 'pygame.Surface' object has no attribute 'x'