Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, math
- pygame.init()
- width = 700
- height = 700
- window = pygame.display.set_mode((width, height), pygame.NOFRAME)
- pygame.display.set_caption("car game")
- img = pygame.image.load("car7_red.png")
- class black:
- def __init__(self, x, y, height, width, color):
- self.x = x
- self.y = y
- self.height = height
- self.width = width
- self.color = color
- self.blackimage = pygame.image.load("downwhite.png")
- self.blackimage = pygame.transform.scale(self.blackimage, (self.blackimage.get_width() //2,self.blackimage.get_height()//2))
- self.rect = pygame.Rect(x, y, height, width) def draw(self):
- self.rect.topleft = (self.x, self.y) window.blit(self.blackimage, self.rect)
- class carline:
- def __init__(self, x, y, height, width, color):
- self.x = x self.y = y self.height = height self.width = width self.color = color self.image = pygame.image.load("downs.png") self.rect = pygame.Rect(x, y, height, width) def draw(self):
- self.rect.topleft = (self.x, self.y) window.blit(self.image, self.rect)
- class hole:
- def __init__(self, x, y, height, width, color):
- self.x = x self.y = y self.height = height self.width = width self.color = color self.holeimage = pygame.image.load("hole.png") self.rect = pygame.Rect(x, y, height, width) def draw(self):
- self.rect.topleft = (self.x, self.y) pygame.draw.rect(window, self.color, self.rect, 2)
- white = (255, 255, 255) hole1 = hole(-10, 500, 468, 60, white)
- class movecar:
- def __init__(self, x, y, height, width, color):
- self.x = x self.y = y self.height = height self.width = width self.color = color self.speed = 3 self.carimage = pygame.image.load("car12.png") self.rect = pygame.Rect(x, y, height, width) def draw(self):
- self.rect.topleft = (self.x, self.y) window.blit(self.carimage, self.rect)
- class movecar2:
- def __init__(self, x, y, height, width, color):
- self.x = x self.y = y self.height = height self.width = width self.color = color self.speed = 3 self.carimage = pygame.image.load("car23.png") self.rect = pygame.Rect(x, y, height, width) self.hitbox = (self.x + 17, self.y + 11, 29, 52)# NEW def draw(self):
- self.rect.topleft = (self.x, self.y) window.blit(self.carimage, self.rect)
- white = (255, 255, 255) car2 = movecar(514, 628, 10, 10, white) car3 = movecar2(389, -100, 10, 10, white)
- class line4:
- def __init__(self, x, y, height, width, color):
- self.x = x self.y = y self.height = height self.width = width self.color = color self.image = pygame.image.load("lineup45.png") self.rect = pygame.Rect(x, y, height, width)
- def draw(self):
- self.rect.topleft = (self.x, self.y) window.blit(self.image, self.rect)
- # -- -- -- -- -- --
- lineup1 = []
- #-- -- -- -- -- --
- blacks = [] carlines = [] platformGroup = pygame.sprite.Group
- level = [
- " s d s",
- " v s d s",
- " s d s",
- " s d s",
- " s d s",
- " s d s",
- " s d s",
- " s d s",
- " s d s",
- " s d s",
- " s d s",
- " s d s",
- " s d s",
- " s d s",
- " s d s",
- " s d s",
- " s d s",
- " s d s"
- ]
- for iy, row in enumerate(level):
- for ix, col in enumerate(row):
- if col == "d":
- new_platforms = black(ix * 9.8, iy * 50, 10, 10, (255, 255, 255)) blacks.append(new_platforms)
- for iy, row in enumerate(level):
- for ix, col in enumerate(row):
- if col == "s":
- new_platforms = carline(ix * 9.8, iy * 50, 10, 10, (255, 255, 255)) carlines.append(new_platforms)
- # === === === === === === === === === === === = Line Ups
- for iy, row in enumerate(level):
- for ix, col in enumerate(row):
- if col == "v":
- new_platforms = line4(ix * -10, iy * -120, 10, 10, (255, 255, 255)) lineup1.append(new_platforms)
- # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
- class Car:
- def __init__(self, x, y, height, width, color):
- self.x = x - width / 2 self.y = y - height / 2 self.height = height self.width = width self.color = color self.rect = pygame.Rect(x, y, height, width) self.surface = pygame.Surface((height, width), pygame.SRCALPHA) self.surface.blit(img, (0, 0)) self.angle = 250 self.speed = 0# 2
- self.hitbox = (self.x + 90, self.y + 620, 370, 63)# NEW pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)
- def draw(self): #3 self.rect.topleft = (int(self.x), int(self.y)) rotated = pygame.transform.rotate(self.surface, self.angle) surface_rect = self.surface.get_rect(topleft = self.rect.topleft) new_rect = rotated.get_rect(center = surface_rect.center) window.blit(rotated, new_rect.topleft) self.hitbox = (self.x, self.y, 70, 40)# NEW pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)
- white = (255, 255, 255) car1 = Car(100, 630, 73, 73, white)# 4 clock = pygame.time.Clock()
- # ReDrawEvertyhing def redraw():
- for carline in carlines:
- carline.draw()
- for black in blacks:
- black.draw()
- # line ups
- for line1 in lineup1:
- line1.draw()
- hole1.draw()
- car1.draw() car3.draw()# moving car car2.draw()
- def movingcars(): #moving cars car2.y -= car2.speed
- if car2.y < -450:
- car2.y = 760 car3.y += car3.speed
- if car3.y > 450:
- car3.y = -390
- runninggame = True
- while runninggame:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- runninggame = False
- movingcars()
- for lineup in lineup1:
- if hole1.rect.colliderect(car1.hitbox):
- car1.speed -= 1
- pressed = pygame.key.get_pressed() car1.speed *= 0.8# 5
- if pressed[pygame.K_UP]: car1.speed += 0.5# 6
- if pressed[pygame.K_DOWN]: car1.speed -= 0.5# 6
- if pressed[pygame.K_LEFT]: car1.angle += car1.speed / 2# 7
- if pressed[pygame.K_RIGHT]: car1.angle -= car1.speed / 2# 7 car1.x -= car1.speed * math.sin(math.radians(car1.angle))# 8 car1.y -= car1.speed * math.cos(math.radians(-car1.angle))# 8
- window.fill((61, 61, 61))# 9
- redraw()
- pygame.display.flip() clock.tick(60)# 10 pygame.quit()
Add Comment
Please, Sign In to add comment