2021. június 23., szerda

Python programozás


















Program forrása:
import turtle # Create screen sc = turtle.Screen() sc.title( "PingPongjatek" ) sc.bgcolor( "white" ) sc.setup(width = 1000 ,
height = 600 )
# Left paddle left_pad = turtle.Turtle() left_pad.speed( 0 ) left_pad.shape( "square" ) left_pad.color( "black" ) left_pad.shapesize(stretch_wid = 6 ,
stretch_len = 2 ) left_pad.penup() left_pad.goto( - 400 , 0 ) # Right paddle right_pad = turtle.Turtle() right_pad.speed( 0 ) right_pad.shape( "square" ) right_pad.color( "black" ) right_pad.shapesize(stretch_wid = 6 ,
stretch_len = 2 ) right_pad.penup() right_pad.goto( 400 , 0 ) # Ball of circle shape hit_ball = turtle.Turtle() hit_ball.speed( 40 ) hit_ball.shape( "circle" ) hit_ball.color( "blue" ) hit_ball.penup() hit_ball.goto( 0 , 0 ) hit_ball.dx = 5 hit_ball.dy = - 5

Videó
https://www.youtube.com/watch?v=C6jJg9Zan7w
---------------

1 import pygame 2 3 4 class Player(): 5 def init (self): 6 self.x, self.y = 16, SCR_HEI/2 7 self.speed = 3 8 self.padwid, self.padHei = 8, 64 9 self.score = 0 10 self.scoreFont = pygame.font.Font( , 64) 11 12 def scoring(self): 13 scoreBlit = self.scoreFont.render(str(self.score), 1, (255, 255, 255)) 14 screen.blit(scoreBlit, (32, 16)) 15 if self.score == 10: 16 print 17 exit() 18 19 def movement(self): 20 keys = pygame.key.get_pressed() 21 if keys[pygame.k_w]: 22 self.y -= self.speed 23 elif keys[pygame.k_sl: 24 self.y += self.speed 25 26 if self.y <= 0: 27 self.y = 0 28 elif self.y >= SCR_HEI.64: 29 self.y = SCR_HEI-64 30 31 def draw(self): 32 pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, self.padwid, self.padHei)) 33 34 class Enemy(): 35 def __init__(self): 36 self.x, self.y = SCR_WID-16, SCR HE1/2 37 self.speed = 38 self.padwid, self.padHei = 8, 64 39 self.score = 0 40 self.scoreFont = pygame.font.Font( , 64) 41 42 def scoring(self): 43 scoreBlit = self.scoreFont.render(str(self.score), 1, (255, 255, 255)) 44 screen.blit(scoreBlit, (SCR_HEI+92, 16)) 45 if self.score == 10: 46 print 47 exit() 48 49 def movement(self): 50 keys = pygame.key.get_pressed() 51 if keys[pygame.K UP]: 52 self.y -= seTf.speed 53 elif keys[pygame.K_DOwN]: 54 self.y += self.speed 55 56 if self.y <= 0: 57 self.y = 0 58 elif self.y >= SCR HEI-64: 59 self.y = SCR HEI-64 60 61 def draw(self): 62 pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, self.padwid, self.padHei)) 63 64 class Ball(): 65 def __init(self): 66 self.x,__ self.y = SCRWID/2, SCR_HEI/2 67 self.speed_x = .3 68 self.speed_y = 3 69 self.size = 8 70 71 def movement(self): 72 self.x += self.speed_x 73 self.y += self.speed_y 74 75 #,all col 76 if self.y <= 0: 77 self.speed_y .1 78 elif self.y >= SCR_HEI-self.size: 79 self.speed_y .= -1 80 81 if self.x <= 0: 82 self.__init__() 83 enemy.score += 1 84 elif self.x ).= scR_WID-self.size: 85 self.init() 86 self.speed_x = 87 player.score += 1 88 ##wall col 89 *paddle col 90 #player 91 for n in range(-self.size, player.padHei): 92 if self.y == player.y + n: 93 if self.x <= player.x + player.padwid: 94 self.speed_x •= -1 95 break 96 n += 97 *enemy 98 for n in range(-self.size, enemy.padHei): 99 if self.y == enemy.y + n: 100 if self.x >= enemy.x - enemy.padwid: 101 self.speed_x .= -1 102 break 103 n += 1 104 ##paddle col 105 106 def draw(self): 107 pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, 8, 8)) 108 109 SCRWID, SCR_HEI = 640, 480 110 screen = pygame.display.set_mode((SCR_WID, SCR_HEI)) 111 pygame.display.set caption( 112 pygame.font.init()— 113 clock = pygame.time.Clock() 114 FPS = 60 115 116 ball = Ball() 117 player = Player() 118 enemy = Enemy() 119 120 while True: 121 *process 122 for event in pygame.event.get(): 123 if event.type == pygame.QUIT: 124 print 'Game exited by user. 125 exit() 126 ##process 127 *logic 128 ball.movement() 129 player.movement() 130 enemy.movement() 131 ##logic 132 *draw 133 screen.fill((0, 0, 0)) 134 ball.draw() 135 player.draw() 136 player. scoring() 137 enemy.draw() 138 enemy. scoring() 139 ##dra. 140 141 pygame.display.flip() 142 clock.tick(FPS) 143

Nincsenek megjegyzések:

Megjegyzés küldése