0
私は現在、グレード10のコンピューターサイエンスクラスでゲームを作っています。私のゲームでは、(今のところ)プレーヤーは矢印キーを使って車を上下に動かすことができます。形状はテストのために現時点では1つの正方形です)、それが画面の上部または下部に触れた場合、ゲームは終了し、コードは自動的に繰り返されます。正方形が左から右に移動するのがわかりますが、右から左へ移動する方法はありますか?これまでのところ私のコードです。Pythonで図形のアニメーションをどのように反転させますか?
import pygame
import time
import random
pygame.init()
gameExit=False
display_width=800
display_height=600
car_height=188
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
black = (0,0,0)
white = (255,255,255)
red=(255,0,0)
blue=(0,0,255)
crashed = False
carImg = pygame.image.load('racecar.png')
def car(x,y):
gameDisplay.blit(carImg, (x,y))
def things(thingx, thingy, thingw, thingh, colour):
pygame.draw.rect(gameDisplay, colour, [thingx, thingy, thingw, thingh])
def text_objects(text, font):
textSurface = font.render(text, True, red)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/8))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
message_display('You Crashed')
def game_loop():
x = (display_width * 0)
y = (display_height * 0.68)
gameExit=False
y_change=0
thing_starty=random.randrange(0, display_height)
thing_startx=-300
thing_speed=7
thing_width=100
thing_height=100
while not gameExit:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
#Key bindings
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change = -10
elif event.key == pygame.K_DOWN:
y_change = 10
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
y +=y_change
gameDisplay.fill(white)
# things(thingx, thingy, thingw, thingh, colour)
things(thing_startx, thing_starty, thing_width, thing_height, blue)
thing_startx += thing_speed
car(x,y)
if y > display_height - car_height or y < 0:
crash()
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()