|
| 1 | +import pygame |
| 2 | +import time |
| 3 | +import random |
| 4 | +from tkinter import * |
| 5 | + |
| 6 | +pygame.init() |
| 7 | + |
| 8 | +display_width=800 |
| 9 | +display_height=600 |
| 10 | + |
| 11 | +gamedisplay=pygame.display.set_mode((display_width,display_height)) |
| 12 | +pygame.display.set_caption("Flappy Bird") |
| 13 | + |
| 14 | + |
| 15 | +green = (0,255,0) |
| 16 | +blue = (102,184,252) |
| 17 | +poles_color=(235,255,3) |
| 18 | +poles2_color=(254,200,8) |
| 19 | +poles_ychange=2 |
| 20 | +poles_xchange=2 |
| 21 | + |
| 22 | +clock=pygame.time.Clock() |
| 23 | +crashed=False |
| 24 | +birdimg=pygame.image.load("bird2.png") |
| 25 | +cloudimg=pygame.image.load("clouds.png") |
| 26 | +poles=pygame.image.load("poles.png") |
| 27 | + |
| 28 | +def bird(x,y): |
| 29 | + gamedisplay.blit(birdimg,(x,y)) |
| 30 | +def clouds(x,y): |
| 31 | + gamedisplay.blit(cloudimg,(x,y)) |
| 32 | + |
| 33 | + |
| 34 | +x=(display_width*0.3) |
| 35 | +y=(display_height*0.8) |
| 36 | +y_change=0 |
| 37 | +poles_y=750 |
| 38 | +poles_x=random.randint(0,540) |
| 39 | +second_poles_y=950 |
| 40 | +second_poles_x=random.randint(0,540) |
| 41 | +third_poles_y=1150 |
| 42 | +third_poles_x=random.randint(0,540) |
| 43 | + |
| 44 | +points=0 |
| 45 | + |
| 46 | +while not crashed: |
| 47 | + for event in pygame.event.get(): |
| 48 | + if event.type == pygame.QUIT: |
| 49 | + crashed = True |
| 50 | + if event.type == pygame.KEYDOWN: |
| 51 | + if event.key == pygame.K_UP: |
| 52 | + y_change=10 |
| 53 | + if event.type == pygame.KEYUP: |
| 54 | + if event.key == pygame.K_UP: |
| 55 | + y_change=0 |
| 56 | + y=y-y_change |
| 57 | + if y<550: |
| 58 | + y=y+5 |
| 59 | + |
| 60 | + |
| 61 | + pygame.draw.rect(gamedisplay,blue,(0,0,800,350),0 ) |
| 62 | + pygame.draw.rect(gamedisplay,green,(0,350,800,250),0 ) |
| 63 | + clouds(0,100) |
| 64 | + bird(x,y) |
| 65 | + |
| 66 | + def poles(x,y): |
| 67 | + global poles_y |
| 68 | + global poles_x |
| 69 | + pygame.draw.rect(gamedisplay,poles_color,(y,0,50,x),0 ) |
| 70 | + pygame.draw.rect(gamedisplay,poles2_color,(y-2.5,x,55,10),0 ) |
| 71 | + pygame.draw.rect(gamedisplay,poles_color,(y,x+150,50,600-x),0 ) |
| 72 | + pygame.draw.rect(gamedisplay,poles2_color,(y-2.5,x+140,55,10),0 ) |
| 73 | + if y==-50: |
| 74 | + poles_y=800 |
| 75 | + poles_x=random.randint(0,540) |
| 76 | + |
| 77 | + |
| 78 | + def second_poles(x,y): |
| 79 | + global second_poles_y |
| 80 | + global second_poles_x |
| 81 | + pygame.draw.rect(gamedisplay,poles_color,(y,0,50,x),0 ) |
| 82 | + pygame.draw.rect(gamedisplay,poles2_color,(y-2.5,x,55,10),0 ) |
| 83 | + pygame.draw.rect(gamedisplay,poles_color,(y,x+150,50,600-x),0 ) |
| 84 | + pygame.draw.rect(gamedisplay,poles2_color,(y-2.5,x+140,55,10),0 ) |
| 85 | + if y==-50: |
| 86 | + second_poles_y=800 |
| 87 | + second_poles_x=random.randint(0,540) |
| 88 | + |
| 89 | + def third_poles(x,y): |
| 90 | + global third_poles_y |
| 91 | + global third_poles_x |
| 92 | + pygame.draw.rect(gamedisplay,poles_color,(y,0,50,x),0 ) |
| 93 | + pygame.draw.rect(gamedisplay,poles2_color,(y-2.5,x,55,10),0 ) |
| 94 | + pygame.draw.rect(gamedisplay,poles_color,(y,x+150,50,600-x),0 ) |
| 95 | + pygame.draw.rect(gamedisplay,poles2_color,(y-2.5,x+140,55,10),0 ) |
| 96 | + if y==-50: |
| 97 | + third_poles_y=800 |
| 98 | + third_poles_x=random.randint(0,540) |
| 99 | + |
| 100 | + |
| 101 | + poles(poles_x,poles_y) |
| 102 | + second_poles(second_poles_x,second_poles_y) |
| 103 | + third_poles(third_poles_x,third_poles_y) |
| 104 | + |
| 105 | + poles_y=poles_y-poles_ychange |
| 106 | + second_poles_y=second_poles_y-poles_ychange |
| 107 | + third_poles_y=third_poles_y-poles_ychange |
| 108 | + |
| 109 | + if x == poles_y or x==poles_y+50: |
| 110 | + if y < poles_x +10 or y>poles_x+140: |
| 111 | + crashed=True |
| 112 | + else: |
| 113 | + points=points+1 |
| 114 | + if x == second_poles_y or x==second_poles_y+50: |
| 115 | + if y < second_poles_x +10 or y>second_poles_x +140: |
| 116 | + crashed=True |
| 117 | + else: |
| 118 | + points=points+1 |
| 119 | + if x == third_poles_y or x==third_poles_y+50: |
| 120 | + if y < third_poles_x +10 or y>third_poles_x+140: |
| 121 | + crashed=True |
| 122 | + else: |
| 123 | + points=points+1 |
| 124 | + |
| 125 | + |
| 126 | + pygame.display.update() |
| 127 | + clock.tick(60) |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +window=Tk() |
| 132 | +window.title("Points table") |
| 133 | +label1=Label(window, text="Your Score Is...",padx=10, pady=10,) |
| 134 | +label1.pack() |
| 135 | +label2=Label(window,text=str(points),padx=10, pady=10,) |
| 136 | +label2.pack() |
| 137 | +window.mainloop() |
| 138 | +pygame.quit() |
| 139 | +quit() |
0 commit comments