Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- pygame.init()
- clock = pygame.time.Clock()
- screen = pygame.display.set_mode((800,600))
- pygame.display.set_caption("FLAPPY BIRDS")
- #BIRD
- x = 200
- y = 300
- jump = 0
- speed = 0.5
- def draw_circle(x,y):
- pygame.draw.circle(screen, (255,0,0), (x,y), 30)
- #PIPES
- pipe1 = [800, 0, 50, random.randint(20,250)]
- pipe2 = [400, 0, 50, random.randint(50,250)]
- Pipes = []
- Pipes.append(pipe1)
- Pipes.append(pipe2)
- def draw_pipes(PIPE):
- #left, top, width, heihgt
- #TOP
- pygame.draw.rect(screen, (0,255,0), (PIPE[0], PIPE[1], PIPE[2], PIPE[3]))
- #BOTTOM
- pygame.draw.rect(screen, (0,255,0), (PIPE[0], 150+PIPE[3], PIPE[2], PIPE[3]+400))
- score = 0
- running = True
- while running:
- screen.fill((120,120,255))
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_SPACE: jump = 1
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_SPACE: jump = 0
- #BIRD MOVEMENT
- draw_circle(x,y)
- if jump == 1:
- y -= 3
- else:
- y += speed
- #PIPE MOVEMENT
- for i in Pipes:
- draw_pipes(i)
- i[0] -= 3
- if i[0] < 0:
- i[0] = 800
- i[3] = random.randint(50,250)
- #GAME OVER AND SCORE
- for i in Pipes:
- if i[0] == 200:
- if y <= i[3] or y >= 150+i[3]:
- print("GAME OVER")
- running = False
- elif i[3] < y < 150+i[3]:
- score += 1
- print(score)
- clock.tick(30)
- pygame.display.update()
- ------------------------------------------------------------------------------------------------------
- import pygame
- import random
- pygame.init()
- screen = pygame.display.set_mode((288,512))
- pygame.display.set_caption("FLAPPY BIRDS")
- background = pygame.image.load('imgs/background.png')
- base = pygame.image.load('imgs/base.png')
- #BIRD
- x = 100
- y = 300
- jump = 0
- speed = 2
- birdimg = pygame.image.load('imgs/bird.png')
- def draw_bird(x,y):
- screen.blit(birdimg, (x,y))
- #PIPES
- pipeupimg = pygame.image.load('imgs/pipe-up.png')
- pipedownimg = pygame.image.load('imgs/pipe-down.png')
- pipe1 = [300, -170]
- pipe2 = [550, -100]
- Pipes = []
- Pipes.append(pipe1)
- Pipes.append(pipe2)
- def draw_pipe(PIPE):
- screen.blit(pipeupimg, (PIPE[0], PIPE[1]))
- screen.blit(pipedownimg, (PIPE[0], PIPE[1]+420))
- #SCORE
- score = 0
- font = pygame.font.Font('freesansbold.ttf', 32)
- sCoord = (10,10)
- def print_score(sc):
- screen.blit(font.render("SCORE: " + str(sc), True, (255,255,255)), sCoord)
- running = True
- while running:
- screen.blit(background, (0,0)) #UPPER PART
- #screen.blit(base, (0,410))
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_SPACE: jump = 1
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_SPACE: jump = 0
- #GAME OVER AND SCORE
- for i in Pipes:
- if i[0] == 100:
- if y < i[1] + 320 or y > i[1]+420:
- print("GAME OVER")
- running = False
- elif i[0]+320 < y < i[1]+420:
- score += 1
- print(score)
- #BIRD MOVEMENT
- draw_bird(x,y)
- if jump == 1:
- y -= 2
- else:
- y += speed
- #PIPE MOVEMENT
- #draw_pipe(Pipes[0])
- #draw_pipe(Pipes[1])
- for i in Pipes:
- draw_pipe(i)
- i[0] -= 0.5
- if i[0] < 0:
- i[0] = 500
- i[1] = random.randint(-250,-100)
- screen.blit(base, (0,410)) #LOWER PART
- print_score(score)
- pygame.display.update()
- ------------------------------------------------------------------------------------------------------
- import pygame
- import random
- pygame.init()
- sw = 800
- sh = 600
- screen = pygame.display.set_mode((sw,sh))
- pygame.display.set_caption("PING - PONG")
- def bluescreen():
- bluescreenRunning = True
- while bluescreenRunning:
- screen.fill((0,0,255))
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- bluescreenRunning = False
- pygame.display.update()
- running = True
- while running:
- screen.fill((255,0,0))
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_SPACE:
- running = False
- bluescreen()
- pygame.display.update()
- -----------------------------------------------------------------------------------------------------
- import pygame
- import random
- clock = pygame.time.Clock()
- pygame.init()
- sw = 800
- sh = 600
- screen = pygame.display.set_mode((sw,sh))
- pygame.display.set_caption("PING - PONG")
- ball = pygame.Rect(sw//2 - 15, sh//2 - 15, 30, 30)
- player = pygame.Rect(sw-20, sh//2 - 60, 10, 120)
- opponent = pygame.Rect(10, sh//2 - 60, 10, 120)
- bg_color = pygame.Color('grey12')
- ball_speed_x = 6*random.choice((-1,1))
- ball_speed_y = 6*random.choice((-1,1))
- player_speed = 0
- opponent_speed = 6
- player_score = 0
- opponent_score = 0
- game_font = pygame.font.Font('freesansbold.ttf', 32)
- score_time = None
- def ball_restart():
- global score_time
- ball.center = (sw//2 + 7, sh//2)
- #ball.x = sw//2 - 15
- #ball.y = sh//2 - 15
- current_time = pygame.time.get_ticks()
- if current_time - score_time <= 2000:
- ball_speed_x = 0
- ball_speed_y = 0
- if current_time - score_time > 2000:
- ball_speed_x = 6*random.choice((-1,1))
- ball_speed_y = 6*random.choice((-1,1))
- score_time = None
- running = True
- while running:
- screen.fill(bg_color)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_UP: player_speed -= 7
- if event.key == pygame.K_DOWN: player_speed += 7
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_UP: player_speed += 7
- if event.key == pygame.K_DOWN: player_speed -= 7
- #SCORE
- if ball.left <= 0:
- score_time = pygame.time.get_ticks()
- player_score += 1
- ball_restart()
- if ball.right >= sw:
- score_time = pygame.time.get_ticks()
- opponent_score += 1
- ball_restart()
- if score_time:
- ball_restart()
- #COLLISION
- if ball.colliderect(player) or ball.colliderect(opponent):
- ball_speed_x *= -1
- #BALL MOVEMENT
- ball.x += ball_speed_x
- ball.y += ball_speed_y
- if ball.top <= 0 or ball.bottom >= sh:
- ball_speed_y *= -1
- if ball.left <= 0 or ball.right >= sw:
- ball_speed_x *= -1
- #PLAYER MOVEMENT
- player.y += player_speed
- if player.top <= 0:
- player.top = 0
- if player.bottom >= sh:
- player.bottom = sh
- #OPPONENT MOVEMENT
- if opponent.bottom < ball.y:
- opponent.bottom += opponent_speed
- if opponent.top > ball.y:
- opponent.top -= opponent_speed
- pygame.draw.rect(screen, (200,200,200), player)
- pygame.draw.rect(screen, (200,200,200), opponent)
- pygame.draw.ellipse(screen, (200,200,200), ball)
- pygame.draw.aaline(screen, (200,200,200), (sw//2,0), (sw//2,sh))
- player_text = game_font.render(str(player_score), True, (200,200,200))
- screen.blit(player_text, (sw//2 + 20, sh//2 - 16))
- opponent_text = game_font.render(str(opponent_score), True, (200,200,200))
- screen.blit(opponent_text, (sw//2 - 42, sh//2 - 16))
- clock.tick(30)
- pygame.display.update()
- ------------------------------------------------------------------------------------------------------
Add Comment
Please, Sign In to add comment