Python: some pygame objects do not appear -
i'm building game have dodge bricks on road. blocks appear little white lines in center of window (the lane separators blocks can move) won't , crashes game when load up!
this code:
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: 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 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 , x < thing_startx + thing_width or x + car_width > thing_startx , x + car_width < thing_startx + thing_width: print('x crossover') crash() pygame.display.update() clock.tick(60) game_loop() pygame.quit() quit()
now here error:
python 3.2.1 (default, jul 10 2011, 21:51:15) [msc v.1500 32 bit (intel)] on win32 type "copyright", "credits" or "license()" 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 invalid >>> ================================ restart ================================ >>>
why rect not valid? if works "thing" object, why cant work "middle_road" object? missing something?
your problem middle_road_startx tuple , not int:
middle_road_startx = (0, display_width/2)
looking @ rest of code guess want:
middle_road_startx = random.randrange(0, display_width/2)
edit
however not part of question, in comments express want object in middle of screen, replace these lines:
middle_road_starty = display_height middle_road_speed = 7 # if want static in middle why have speed? middle_road_width = 50 middle_road_height = 75 middle_road_startx = (0, display_width/2)
with:
middle_road_speed = 7 # if want static in middle why 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)
so first calculate middle of screen (display_width/2)
,(display_height/2)
, offset width/height of object wanna draw there (middle_road_width/2)
,(middle_road_height/2)
Comments
Post a Comment