我正在制作一个10年级计算机科学课程的游戏,我的游戏是(到目前为止)玩家用箭头键上下移动汽车,同时避免不同的形状(现在目前只有形状是一个正方形用于测试),如果它接触到屏幕的顶部或底部,则游戏结束并且代码自动重复。我看到方块从左向右移动,但有没有办法让它从右向左移动?到目前为止,这是我的代码。
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()
答案 0 :(得分:0)
将thing_startx=-300
更改为thing_startx = display_width
,将thing_speed = 7
更改为thing_speed = -7
。