我用pycharm制作了这个python程序。我的目标是使用从块继承的两个精灵图像在 640x360 屏幕上弹跳。虽然,其中一个精灵图像似乎反复弹跳并且似乎从未超过屏幕的中间部分。我认为问题出在我的 refactorcode.py 中的 for 循环中的条件语句对精灵进行编码。我试着改变它,但它似乎不起作用。
重构代码.py
import pygame
from ball import Ball, Block, Sprite
pygame.init()
screen_width = 640
screen_height = 360
screen = pygame.display.set_mode((screen_width, screen_height))
backgrounds = [(0,0,0),(128,0,0),(0,128,0),(0,0,128)]
bgindex = 0
balls = [Ball(screen_width // 2, screen_height // 2, 5, 5, 100, 200, 200, 20),
Ball(screen_width // 4, screen_width // 4, 3, 3, 255, 0, 0, 10),
Ball(300, 300, 7, 7, 0, 255, 0, 15),
Ball(400, 400, 2, 2, 0, 0, 255, 25),
Ball(500, 500, 4, 4, 200, 200, 0, 30),
Block(300,200,7,4,255,0,0,32,2),
Block(300,200,7,4,255,0,0,32,30),
Block(300,200,9,10,255,0,0,5,18),
Block(300,200,7,2,4,0,0,32,18)]
user_ball_speed = 2
clock = pygame.time.Clock()
running = True
sprites = [Sprite(200, 100, 5, 5, "spaceship.png"),
Sprite(400, 200, 5, 5, "spaceship.png")]
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(backgrounds[bgindex])
# Move the user ball
balls[0].vx = 0
balls[0].vy = 0
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
balls[0].vx = -user_ball_speed
if keys[pygame.K_RIGHT]:
balls[0].vx = user_ball_speed
if keys[pygame.K_UP]:
balls[0].vy = -user_ball_speed
if keys[pygame.K_DOWN]:
balls[0].vy = user_ball_speed
for ball in balls:
ball.move(screen_width, screen_height)
ball.render(screen)
if ball.x == balls[0].x and ball.y == balls[0].y:
continue
for sprite in sprites:
sprite.move(screen_width, screen_height)
sprite.render(screen)
if sprite.x < sprite.width / 2:
sprite.x = sprite.width / 2
sprite.vx = -sprite.vx
elif sprite.x > screen_width - sprite.width / 2:
sprite.x = screen_width - sprite.width / 2
sprite.vx = -sprite.vx
if sprite.y < sprite.height / 2:
sprite.y = sprite.height / 2
sprite.vy = -sprite.vy
elif sprite.y > screen_height - sprite.height / 2:
sprite.y = screen_height - sprite.height / 2
sprite.vy = -sprite.vy
pygame.display.flip()
clock.tick(60)
# Clean up Pygame
pygame.quit()
球.py
import pygame
from PIL import Image
class Ball:
def __init__(self, x, y, vx, vy, r, g, b, radius):
self.x = x
self.y = y
self.vx = vx
self.vy = vy
self.r = r
self.g = g
self.b = b
self.radius = radius
def move(self, screen_width, screen_height):
self.x += self.vx
self.y += self.vy
if self.x - self.radius < 0:
self.x = self.radius
self.vx = -self.vx
self.r = (self.r + 27) % 255
self.g = (self.g + 54) % 255
self.b = (self.b + 81) % 255
elif self.x + self.radius > screen_width:
self.x = screen_width - self.radius
self.vx = -self.vx
self.r = (self.r + 27) % 255
self.g = (self.g + 54) % 255
self.b = (self.b + 81) % 255
if self.y - self.radius < 0:
self.y = self.radius
self.vy = -self.vy
self.r = (self.r + 27) % 255
self.g = (self.g + 54) % 255
self.b = (self.b + 81) % 255
elif self.y + self.radius > screen_height:
self.y = screen_height - self.radius
self.vy = -self.vy
self.g = (self.g + 54) % 255
self.b = (self.b + 81) % 255
def render(self, screen):
pygame.draw.circle(screen, (self.r, self.g, self.b), (self.x, self.y), self.radius)
class Block:
def __init__(self,x,y,vx,vy,r,g,b,width,height):
self.x = x
self.y = y
self.vx = vx
self.vy = vy
self.r = r
self.g = g
self.b = b
self.width = width
self.height = height
def move(self, screen_width, screen_height):
self.x += self.vx
self.y += self.vy
if self.x - self.width < 0:
self.x = self.width
self.vx = -self.vx
self.r = (self.r + 27) % 255
elif self.x + self.width > screen_width:
self.x = screen_width - self.width
self.vx = -self.vx
self.r = (self.r + 27) % 255
if self.y - self.height < 0:
self.y = self.height
self.vy = -self.vy
self.g = (self.g + 54) % 255
elif self.y + self.height > screen_height:
self.y = screen_height - self.height
self.vy = -self.vy
self.b = (self.b + 81) % 255
def render(self, screen):
pygame.draw.rect(screen, (self.r, self.g, self.b), (self.x, self.y, self.width, self.height))
# class Sprite(Block):
# def __init__(self, x, y, vx, vy):
# super().__init__(x, y, vx, vy)
# self.image = pygame.image.load("spaceship.png")
# self.rect = self.image.get_rect()
# self.rect.center = (self.x, self.y)
#
# def render(self, screen):
# screen.blit(self.image, (self.x, self.y))
class Sprite(Block):
def __init__(self, x, y, vx, vy, filename):
self.image = pygame.image.load(filename)
self.width, self.height = self.image.get_rect().size
super().__init__(x, y, vx, vy, 255, 255, 255, self.width, self.height)
def move(self, screen_width, screen_height):
self.x += self.vx
self.y += self.vy
self.x += self.vx
self.y += self.vy
if self.x < 0:
self.x = 0
self.vx = -self.vx
elif self.x + self.width > screen_width:
self.x = screen_width - self.width
self.vx = -self.vx
if self.y < 0:
self.y = 0
self.vy = -self.vy
elif self.y + self.height > screen_height:
self.y = screen_height - self.height
self.vy = -self.vy
def render(self, surface):
surface.blit(self.image, (self.x, self.y))