我正在尝试制作一个移动平台,所以当平台到达帷幕点时,它应该反转方向并返回但是从我所看到的它看起来像是振动回来和堡垒 节目链接:https://drive.google.com/file/d/0BzvvQCByWwmAQThfdkEtSlRKa1k/view?usp=sharing
这是我的代码:
div.video_div{
width:800px;
height:300px;
border:1px solid red;
background-color:blue;
margin:0 auto;
overflow: hidden; /* Add this */
}
video{
width:100%;
height:100%;
/* Use contain instead if you want the object to fit without cropping */
object-fit: cover;
}
答案 0 :(得分:2)
您的问题在spd = 5
内while True
。
您可以使用
更改方向spd = -spd
但之后用
覆盖它spd = 5
您必须在spd = 5
while True
完整版和其他修改。
import pygame
import sys
# --- constants --- (UPPER_CASE names)
WHITE = (255, 255, 255)
GREEN = (20, 255, 140)
GREY = (210, 210 ,210)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)
BLACK = (0,0,0)
SCREEN_WIDTH=1000
SCREEN_HEIGHT=700
# --- classes --- (CamelCase names)
class LBuild(pygame.sprite.Sprite):
def __init__(self, color, width, height, x, y):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
# Draw the car (a rectangle!)
pygame.draw.rect(self.image, color, [0, 0, width, height])
# Fetch the rectangle object that has the dimensions of the image.
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# --- functions --- (lower_case names)
def level_1(screen, all_sprites_list, movblock):
x = 0
for _ in range(50):
all_sprites_list.add(LBuild(GREY, 20, 20, x, 680))
x += 20
y = 660
x2 = 40
for _ in range(2):
all_sprites_list.add(LBuild(BLACK, 60, 20, x2, y))
y -= 20
mblk = LBuild(RED, 100, 20, 120, 600)
movblock.add(mblk)
spd = 5
# - mainloop -
clock = pygame.time.Clock()
#current_time = pygame.time.get_ticks()
# change something after 2s
#change_time = current_time + 2000 # 2000ms = 2s
while True:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
# False = exit game
return False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
# True = go to next level
return True
# - updates (without draws) -
#current_time = pygame.time.get_ticks()
#if current_time >= change_time:
# TODO: change something
# # change something again after 2s
# change_time = current_time + 2000
all_sprites_list.update()
if mblk.rect.x > 200:
spd = -spd
if mblk.rect.x < 100:
spd = -spd
mblk.rect.x += spd
# - draws (without updates) -
screen.fill(WHITE)
all_sprites_list.draw(screen)
movblock.draw(screen)
pygame.display.flip()
# - FPS -
clock.tick(60)
# --- main ---
# - init -
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Car Racing")
# - game -
all_sprites_list = pygame.sprite.Group()
movblock = pygame.sprite.Group()
goto_next_level = level_1(screen, all_sprites_list, movblock)
#if goto_next_level:
# goto_next_level = level_2(screen, all_sprites_list, movblock)
#if goto_next_level:
# goto_next_level = level_3(screen, all_sprites_list, movblock)
# - exit -
pygame.quit()
sys.exit()