#include <iostream> using namespace std; int main() { cout << "Hello, World!"; return 0; }import pygame import sys # Game variables gravity = 0.25 bird_movement = 0 bird_rect = bird_surface.get_rect(center = (100,512)) def draw_floor(): screen.blit(floor_surface,(floor_x_pos,900)) screen.blit(floor_surface,(floor_x_pos + 576,900)) def create_pipe(): random_pipe_pos = random.choice(pipe_height) bottom_pipe = pipe_surface.get_rect(midtop = (700,random_pipe_pos)) top_pipe = pipe_surface.get_rect(midbottom = (700,random_pipe_pos - 300)) return bottom_pipe,top_pipe def move_pipes(pipes): for pipe in pipes: pipe.centerx -= 5 return pipes def draw_pipes(pipes): for pipe in pipes: if pipe.bottom >= 1024: screen.blit(pipe_surface,pipe) else: flip_pipe = pygame.transform.flip(pipe_surface,False,True) screen.blit(flip_pipe,pipe) def check_collision(pipes): for pipe in pipes: if bird_rect.colliderect(pipe): return False if bird_rect.top <= -100 or bird_rect.bottom >= 900: return False return True def rotate_bird(bird): new_bird = pygame.transform.rotozoom(bird,-bird_movement * 3,1) return new_bird def bird_animation(): new_bird = bird_frames[bird_index] new_bird_rect = new_bird.get_rect(center = (100,bird_rect.centery)) return new_bird,new_bird_rect def score_display(game_state): if game_state == 'main_game': score_surface = game_font.render(str(int(score)),True,(255,255,255)) score_rect = score_surface.get_rect(center = (288,100)) screen.blit(score_surface,score_rect) if game_state == 'game_over': score_surface = game_font.render(f'Score: {int(score)}' ,True,(255,255,255)) score_rect = score_surface.get_rect(center = (288,100)) screen.blit(score_surface,score_rect) high_score_surface = game_font.render(f'High score: {int(high_score)}',True,(255,255,255)) high_score_rect = high_score_surface.get_rect(center = (288,850)) screen.blit(high_score_surface,high_score_rect) def update_score(score, high_score): if score > high_score: high_score = score return high_score # Pygame init pygame.init() screen = pygame.display.set_mode((576,1024)) clock = pygame.time.Clock() game_font = pygame.font.Font('04B_19.ttf',40) # Game loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE and game_active: bird_movement = 0 bird_movement -= 12 if event.key == pygame.K_SPACE and game_active == False: game_active = True pipe_list.clear() bird_rect.center = (100,512) bird_movement = 0 score = 0 if event.type == BIRDFLAP: if bird_index < 2: bird_index += 1 else: bird_index = 0 bird_surface,bird_rect = bird_animation() screen.blit(bg_surface,(0,0)) if game_active: # Bird bird_movement += gravity rotated_bird = rotate_bird(bird_surface) bird_rect.centery += bird_movement screen.blit(rotated_bird,bird_rect) game_active = check_collision(pipe_list) # Pipes pipe_list = move_pipes(pipe_list) draw_pipes(pipe_list) score += 0.01 score_display('main_game') else: screen.blit(game_over_surface,game_over_rect) high_score = update_score(score,high_score) score_display('game_over') # Floor floor_x_pos -= 1 draw_floor() if floor_x_pos <= -576: floor_x_pos = 0 pygame.display.update() clock.tick(120)
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}