我正在建造一个你必须在道路上躲避砖块的游戏。块会出现但窗口中央的小白线(通道分隔符是块,所以它们可以移动)不会,当我加载它时会崩溃游戏!
这是代码:
import pygame
import time
import random
pygame.init()
#Colour Acronym Chart
#| Black = Blck | Grey = Gry | Dark = Drk | White = Wht
#| Deep = Dp | Metal = Mtl | Light = Lht | Cyan = Cyn
#| Blue = Bl | Baby = Bby | Maroon = Mrn |
#| Red = Rd | Fire = Fr | Orange = Orng |
# Window Colour Index
Wht = (255, 255, 255)
Blck = (0, 0, 0)
Dp_Gry = (32, 32, 32)
Mtl_Gry = (96, 96, 96)
Lht_Gry = (160, 160, 160)
Dp_Bl = (0, 0, 102)
Lht_Bby_Bl = (0, 128, 255)
Cyn = (0, 153, 153)
Drk_Mrn = (102, 0, 0)
Fr_Rd = (255, 0, 0)
Lht_Orng = (255, 128, 0)
#END OF COLOUR MODULE
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Hard Drive')
clock = pygame.time.Clock()
bg = pygame.image.load("asphalt.jpg")
carImg = pygame.image.load('car1.png')
car_width = 45
car_height = 45
def things(thingx, thingy, thingw, thingh, color):
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
def middle_road(roadx, roady, roadw, roadh, color):
pygame.draw.rect(gameDisplay, color, [roadx, roady, roadw, roadh])
def car(x,y):
gameDisplay.blit(carImg,(x,y))
def text_objects(text, font):
textSurface = font.render(text, True, Blck)
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/2))
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.45)
y = (display_height * 0.8)
x_change = 0
thing_starty = -600
thing_speed = 7
thing_width = 100
thing_height = 100
middle_road_starty = display_height
middle_road_speed = 7
middle_road_width = 50
middle_road_height = 75
middle_road_startx = (0, display_width/2)
thing_startx = random.randrange(0, display_width)
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(Wht)
#INSIDE OF THE GAME LOOP
gameDisplay.blit(bg, (0, 0))
#thingx, thingy, thingw, thingh, color
things(thing_startx, thing_starty, thing_width, thing_height, Cyn)
thing_starty += thing_speed
car(x,y)
middle_road(middle_road_startx, middle_road_starty, middle_road_width, middle_road_height, Wht)
middle_road_starty += middle_road_speed
if x > display_width - car_width or x < 0:
crash()
if thing_starty > display_height:
thing_starty = 0 - thing_height
thing_startx = random.randrange(0, display_width)
if y < thing_starty+thing_height:
print('y crossover')
if x > thing_startx and x < thing_startx + thing_width or x + car_width > thing_startx and x + car_width < thing_startx + thing_width:
print('x crossover')
crash()
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
现在出现错误:
Python 3.2.1 (default, Jul 10 2011, 21:51:15) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "C:\Users\Aidan\Desktop\PyGame\frame.py", line 144, in <module>
game_loop()
File "C:\Users\Aidan\Desktop\PyGame\frame.py", line 123, in game_loop
middle_road(middle_road_startx, middle_road_starty, middle_road_width, middle_road_height, Wht)
File "C:\Users\Aidan\Desktop\PyGame\frame.py", line 48, in middle_road
pygame.draw.rect(gameDisplay, color, [roadx, roady, roadw, roadh])
TypeError: Rect argument is invalid
>>> ================================ RESTART ================================
>>>
为什么Rect无效?如果它适用于“thing”对象,为什么它不能用于“middle_road”对象?我错过了什么吗?
答案 0 :(得分:1)
你的问题是middle_road_startx是一个元组而不是int:
middle_road_startx = (0, display_width/2)
看看剩下的代码,我想这就是你想要的:
middle_road_startx = random.randrange(0, display_width/2)
修改强>
然而,不是您的问题的一部分,在您表达希望对象位于屏幕中间的评论中,请替换以下行:
middle_road_starty = display_height
middle_road_speed = 7 # if you want it static in the middle why would it have speed?
middle_road_width = 50
middle_road_height = 75
middle_road_startx = (0, display_width/2)
使用:
middle_road_speed = 7 # if you want it static in the middle why would it have speed?
middle_road_width = 50
middle_road_height = 75
middle_road_startx = (display_width/2) - (middle_road_width/2)
middle_road_starty = (display_height/2) - (middle_road_height/2)
首先计算屏幕中间(display_width/2)
,(display_height/2)
,然后偏移您想要在那里绘制的对象的宽度/高度(middle_road_width/2)
,(middle_road_height/2)