Пожалуйста, помогите, я пытаюсь получить мой космический корабль, который является классом Игрока, для перемещения вверх и вниз. Я несколько раз меняю вещи и до сих пор не повезло. У кого-нибудь есть идеи, что я делаю неправильно?Spaceship Sprite не двигается вверх и вниз в Pygame
import pygame, sys, random
from pygame.locals import *
def isPointInsideRect(x, y, rect):
if ((x > rect.left) and \
(x < rect.right) and \
(y > rect.top) and \
(y < rect.bottom)):
return True
else:
return False
#def doRectsOverlap(spaceship, Meteor):
#for a,b in [(spaceship, Meteor), (spaceship, Meteor)]:
# check if a's corners are inside b
# if ((isPointInsideRect(a.left, a.top, b)) or
# (isPointInsideRect(a.left, a.bottom, b)) or
# (isPointInsideRect(a.right, a.top, b)) or
# (isPointInsideRect(a.right, a.bottom, b))):
# return True
# return False
class Meteor:
def __init__(self, x, y, file):
self._x = x
self._y = y
self._img = pygame.image.load(file)
self._w = self._img.get_rect().width
self._h = self._img.get_rect().height
self._dx = 5
self._dy = 1
self._rect = pygame.Rect(self._x, self._y, self._w, self._h)
def render(self, ds):
ds.blit(self._img, (self._x, self._y))
def get_rect(self):
return self._rect
def move(self):
self._x -= self._dx
self._rect = pygame.Rect(self._x, self._y, self._w,self._h)
if self._x < 0:
return True
return False
class Player:
def __init__(self, x, y, file):
self._x = x
self._y = y
self._img = pygame.image.load(file)
self._w = self._img.get_rect().width
self._h = self._Img.get_rect().height
self._down = False
self._up = False
self._dx = 1
self._dy = 1
self._rect = pygame.Rect(self._x, self._y, self._w, self._h)
def render(self, ds):
ds.blit(self._img, (self._x, self._y))
def get_rect(self):
return self._rect
def get_input(self, keystate, direction):
if (keystate == KEYDOWN) and (direction == K_UP):
self._up = True
elif (keystate == KEYUP) and (direction == K_UP):
self._up = False
elif (keystate == KEYDOWN) and (direction == K_DOWN):
self._down = True
elif (keystate == KEYUP) and (direction == K_DOWN):
self._down = False
def move(self):
if (self._up == True) and (self._y > 0):
self._y-=3
if (self._down == True) and (self._y < (500 - self._h)):
self._y+=3
self._rect = pygame.Rect(self._x, self._y, self._w,self._h)
def reset(self):
self.up = False
self.down = False
pygame.init()
# configure a frame rate timer
FPS=60
fpsclock = pygame.time.Clock()
# set up the window
displaysurf = pygame.display.set_mode((800, 500), 0, 32)
pygame.display.set_caption('Meteor Dodge')
# set up the colors
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
objects = []
#for i in range(50):
#Objects.append(Meteor(random.randint(10,490), random.randint(10,90), "meteor.png"))
spacerock = pygame.image.load("meteor.png")
spaceship = player(0, 200 , "Spaceship.png")
pygame.mixer.music.load("bjorn__lynne-_12th_warrior.mid")
pygame.mixer.music.play(-1, 0.0)
hitsound = pygame.mixer.Sound("match0.wav")
loop = 0
# run the game loop
while True:
displaysurf.fill(BLACK)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
spaceship.move()
spaceship.render(displaysurf)
loop = loop + 1
if loop == 50:
objects.append(Meteor(690, random.randint(10,490), "meteor.png"))
loop = 0
for object in objects[:]:
if object.move():
objects.remove(object)
object.render(displaysurf)
#for Object in Objects[:]:
# if doRectsOverlap(Meteor.get_rect(), spaceship.get_rect()):
# objects.remove(spaceship)
pygame.display.update()
fpsclock.tick(FPS)
исправить отступы. – letsc
вы должны использовать функцию 'move_ip()'. Вы можете прочитать об этом [здесь] (https://www.pygame.org/docs/ref/rect.html). Это сделает вашу жизнь намного проще или, по крайней мере, для меня. – SirParselot
- это 'isPointInsideRect' - повторная реализация' Rect.collidepoint'? http://www.pygame.org/docs/ref/rect.html#pygame.Rect.collidepoint – cmd