Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- pygame.init()
- clock = pygame.time.Clock()
- sw = 800
- sh = 600
- screen = pygame.display.set_mode((sw,sh))
- pygame.display.set_caption("PING--PONG")
- bg_color = pygame.Color('grey12')
- game_font = pygame.font.Font('freesansbold.ttf', 60)
- level = 1
- opponent_speed = 6
- score_time = None
- def StartGame(OS):
- global score_time
- sw = 800
- sh = 600
- 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 = OS
- 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()
- welcomscreen = True
- while welcomscreen:
- screen.fill(bg_color)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- welcomscreen = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_SPACE:
- welcomscreen = False
- StartGame(opponent_speed)
- if event.key == pygame.K_1:
- level = 1
- opponent_speed = 6
- elif event.key == pygame.K_2:
- level = 2
- opponent_speed = 10
- elif event.key == pygame.K_3:
- level = 3
- opponent_speed = 13
- #pygame.draw.rect(screen, (250,0,0), pygame.Rect(sw//2, sh-140, 350, 70), 2)
- if level == 1:
- pygame.draw.rect(screen, (250,0,0), pygame.Rect(sw//2-190, sh-410, 350, 70), 2)
- elif level == 2:
- pygame.draw.rect(screen, (250,0,0), pygame.Rect(sw//2-190, sh-310, 350, 70), 2)
- elif level == 3:
- pygame.draw.rect(screen, (250,0,0), pygame.Rect(sw//2-190, sh-210, 350, 70), 2)
- Welcome_message = game_font.render("PING PONG", True, (200,200,200))
- screen.blit(Welcome_message, (sw//2 - 170, 20))
- Select_level = game_font.render("SELECT LEVEL", True, (200,200,200))
- screen.blit(Select_level, (sw//2 - 200, sh-500))
- Easy = game_font.render("EASY", True, (200,200,200))
- screen.blit(Easy, (sw//2 - 90, sh-400))
- Medium = game_font.render("MEDIUM", True, (200,200,200))
- screen.blit(Medium, (sw//2-130, sh-300))
- Hard = game_font.render("HARD", True, (200,200,200))
- screen.blit(Hard, (sw//2 - 90, sh-200))
- Start_game = game_font.render("PRESS SPACE TO START", True, (200,200,200))
- screen.blit(Start_game, (sw//2-360, sh-100))
- clock.tick(30)
- pygame.display.update()
- -------------------------------------------------------------------------------------------------
- import pygame
- import random
- clock = pygame.time.Clock()
- black = (0,0,0)
- red = (255,0,0)
- white = (255,255,255)
- class Block(pygame.sprite.Sprite):
- def __init__(self, color, width, height):
- super().__init__()
- self.image = pygame.Surface([width,height])
- self.image.fill(color)
- self.rect = self.image.get_rect()
- pygame.init()
- sw = 700
- sh = 400
- screen = pygame.display.set_mode((sw,sh))
- block_list = pygame.sprite.Group()
- all_sprites_list = pygame.sprite.Group()
- for i in range(10):
- block = Block(black, 30, 30)
- block.rect.x = random.randrange(sw)
- block.rect.y = random.randrange(sh)
- block_list.add(block)
- all_sprites_list.add(block)
- player = Block(red, 30,30)
- all_sprites_list.add(player)
- score = 0
- running = True
- while running:
- screen.fill(white)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- pos = pygame.mouse.get_pos()
- player.rect.x = pos[0]
- player.rect.y = pos[1]
- blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
- for block in blocks_hit_list:
- score += 1
- print(score)
- all_sprites_list.draw(screen)
- clock.tick(20)
- pygame.display.update()
- -----------------------------------------------------------------------------------------------
- import pygame
- import random
- clock = pygame.time.Clock()
- pygame.init()
- screen = pygame.display.set_mode((200,100))
- pygame.display.set_caption("TILE SMASHER")
- game_font = pygame.font.Font('freesansbold.ttf', 60)
- running = True
- while running:
- screen.fill((200,200,200))
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.MOUSEBUTTONDOWN:
- if event.button == 1: #LEFT
- print("LEFT CLICK")
- elif event.button == 2: #MIDDLE
- print("MIDDLE CLICK")
- elif event.button == 3: #RIGHT
- print("RIGHT")
- elif event.button == 4: #WHEEL UP
- print("WHEEL UP")
- elif event.button == 5: #WHEEL DOWN
- print("WHEEL DOWN")
- clock.tick(10)
- pygame.display.update()
- --------------------------------------------------------------------------------------------
- import pygame, random
- pygame.init()
- clock = pygame.time.Clock()
- sw = 800
- sh = 600
- screen = pygame.display.set_mode((sw,sh))
- pygame.display.set_caption("TILES")
- game_font = pygame.font.Font('freesansbold.ttf', 60)
- xy = [100,100]
- score = 0
- font = pygame.font.Font('freesansbold.ttf', 32)
- sCoord = (10,10)
- def score_print(sc):
- screen.blit(font.render("SCORE: " + str(sc), True, (0,0,0)), sCoord)
- def generate_box(x,y):
- return(pygame.Rect(x,y,100,100))
- def isClicked(cxy, cmx, cmy):
- if cxy[0] < cmx < cxy[0] + 100 and cxy[1] < cmy < cxy[1] + 100:
- return True
- return False
- start_time = pygame.time.get_ticks()
- clicked = False
- running = True
- while running:
- screen.fill((200,200,200))
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.MOUSEBUTTONDOWN:
- if event.button == 1:
- clicked = True
- if event.type == pygame.MOUSEBUTTONUP:
- if event.button == 1:
- clicked = False
- #RANDOM SPAWNING CODE
- current_time = pygame.time.get_ticks()
- if current_time - start_time > 2000:
- start_time = pygame.time.get_ticks()
- xy = [random.randint(50,700), random.randint(50,500)]
- #DETECTION
- mx, my = pygame.mouse.get_pos()
- if clicked:
- if (current_time - start_time < 2000) and isClicked(xy, mx, my):
- start_time = pygame.time.get_ticks()
- xy = [random.randint(50,700), random.randint(50,500)]
- score += 1
- print(score)
- box = generate_box(xy[0], xy[1])
- pygame.draw.rect(screen, (255,0,0), box)
- score_print(score)
- clock.tick(20)
- pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement