python - 使用 Pygame 自行移动对象
问题描述
我正在尝试使用 Pygame 使圆圈从屏幕中间移动到顶部,然后回到中间,依此类推。
import pygame
import sys
pygame.init()
gameOver = False
speed = 5
clock = pygame.time.Clock()
fps = 20
class Screen:
largeur = 600
hauteur = 600
demiLargeur = int(largeur/2)
demiHauteur = int(hauteur/2)
screen = pygame.display.set_mode((Screen.largeur, Screen.hauteur))
class Couleurs:
bleu = (000,000,255)
noir = (000,000,000)
class Cercle:
diametre = 50
epaisseur = 5
posTop = [Screen.demiLargeur, Screen.demiHauteur-2*diametre]
class CirclePos:
top = [Cercle.posTop[0],Cercle.posTop[1]]
circleListTop = [CirclePos.top]
class DrawCircles:
def top (circleListTop):
for CirclePos.top in circleListTop:
pygame.draw.circle(screen, Couleurs.bleu, (CirclePos.top[0],CirclePos.top[1]),Cercle.diametre,Cercle.epaisseur)
class UpdateCirclesPositions:
def top(circleListTop):
for idx,CirclePos.top in enumerate(circleListTop):
if CirclePos.top[1] > Cercle.diametre :
CirclePos.top[1] -= speed
else:
circleListTop.pop(idx)
while not gameOver:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(Couleurs.noir)
clock.tick(fps)
DrawCircles.top(circleListTop)
UpdateCirclesPositions.top(circleListTop)
pygame.display.update()
到目前为止,我有这个代码,想法是让它向上移动,让它消失,然后创建另一个列表来将圆圈从顶部移动到中间。我觉得这是个坏主意。
任何想法 ?
谢谢
解决方案
至于我,你的课太多了。在类 Circle 你应该有它的属性和方法update
和draw
.
当圆圈在顶部时,它应该改变速度 - speed = -speed
。中间的时候也是一样。这样它就可以一直移动。但是它可能需要变量direction
来检查它是否已经改变了方向,因为它可以一直在改变速度的地方(在每一次移动中)。
import pygame
# --- constans ---
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
FPS = 20
SPEED = 5
# --- classes ---
class Colors:
blue = (000, 000, 255)
black = (000, 000, 000)
red = (255, 000, 000)
class Circle:
def __init__(self, x, y, size=50, thick=5, color=Colors.blue, speed=SPEED):
self.size = size
self.thick = thick
self.color = color
self.rect = pygame.Rect(0, 0, 2*size, 2*size)
self.rect.centerx = x
self.rect.centery = y
if speed >= 0:
self.direction = 'up'
else:
self.direction = 'down'
self.speed = speed
def draw(self, screen):
pygame.draw.circle(screen, self.color, self.rect.center, self.size, self.thick)
def update(self):
self.rect.y -= self.speed
if self.rect.top <= 0 and self.direction == 'up':
self.direction = 'down'
self.speed = -self.speed
if self.rect.bottom >= screen_rect.centery and self.direction == 'down':
self.direction = 'up'
self.speed = -self.speed
# --- main ---
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen_rect = screen.get_rect()
circles = [
Circle(screen_rect.centerx, screen_rect.centery),
Circle(screen_rect.centerx, 0, speed=-SPEED, color=Colors.red)
]
clock = pygame.time.Clock()
game_over = False
while not game_over:
# --- events ----
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
# --- updates --- (without draws)
for item in circles:
item.update()
# --- draws --- (without updates)
screen.fill(Colors.black)
for item in circles:
item.draw(screen)
pygame.display.update()
clock.tick(FPS)
pygame.quit() # close window
推荐阅读
- performance - QML 为大量矩形设置动画时性能缓慢
- node.js - /node_modules/angular2-moment/add.pipe.d.ts 中声明的符号 AddPipe 中的错误未从 angular2-moment/moment.module 导出
- amazon-web-services - 我可以阻止 DynamoDB 全局表的副本同步吗?
- c++ - 用于 C++ 编译的 Visual Studio Code 配置
- azure - Azure ML 发布错误 AZUREML_COMPUTE_USE COMMON_RUNTIME
- node.js - 在 AWS lambda 中对 cognito 用户进行身份验证不会调用 authenticateUser() 并继续跳过它
- python - 使用 selenium python 处理网站错误
- java - 如何使用 Mockito 模拟记录
- c# - 尽管连接关闭,但运行命令 adomd
- mysql - 获取特定时间范围内的数据总和