اذهب إلى المحتوى
  • 0

Pygame

Beshoy Ehab3

السؤال

لماذا في هذا الكود الزرين w و s لا يعملان

و اذا كان عندك أي ملحوظه لتحسين الكود قلها لي 

import pygame

from random import randint

pygame.init()

clock = pygame.time.Clock()

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)

GREEN = (0, 185, 0)

BLUE = (0, 0, 255)

RED = (255, 0, 0)

screenWidth = 700

screenHeight = 500

screen = pygame.display.set_mode((screenWidth, screenHeight))

pygame.display.set_caption("Ping Pong")

score1 = 0

score2 = 0

dir = 1

class Madrab:

 def __init__(self, x, y, width, height, color, step):

  self.x = x

  self.y = y

  self.start_y = y

  self.start_x = x

  self.width = width

  self.height = height

  self.color = color

  self.step = step

  self.hitbox = (self.x, self.y, self.width, self.height)

 def draw(self, screen):

  pygame.draw.rect(screen, self.color,(self.x, self.y, self.width, self.height))

  self.hitbox = (self.x, self.y, self.width, self.height)

  #pygame.draw.rect(screen, BLACK, self.hitbox, 1)

class Ball:

 def __init__(self, x, y, radius, color, stepx, stepy):

  self.x = x

  self.y = y

  self.start_x = x

  self.start_y = y

  self.stepy = stepy

  self.stepx = stepx

  self.radius = radius

  self.color = color

 def draw(self, screen):

  pygame.draw.circle(screen, self.color,(self.x, self.y) ,self.radius)

score = pygame.font.SysFont("Arial", 50)

ball = Ball(screenWidth // 2, screenHeight // 2, 10, BLACK, 3, 3)

m1 = Madrab(60, 200, 20, 100, RED, 7)

m2 = Madrab(620, 200, 20, 100, BLUE, 7)

bg = Madrab(0, 0, screenWidth, screenHeight, WHITE, 0)

goul = 20

def redraw():

 text1 = score.render(f"P1 = {score1}", True, GREEN)

 text2 = score.render(f"P2 = {score2}", True, GREEN)

 bg.draw(screen)

 screen.blit(text1, (80, 20))

 screen.blit(text2, (480, 20))

 m1.draw(screen)

 m2.draw(screen)

 ball.draw(screen)

 pygame.display.update()

on = True

while on:

 clock.tick(30)

 keys = pygame.key.get_pressed()

 if keys[pygame.K_DOWN]:

  if m2.y + 100 + m2.step < screenHeight:

   m2.y += m2.step

 elif keys[pygame.K_UP]:

  if m2.y - m2.step > 0:

   m2.y -= m2.step

 if pygame.key.get_pressed()[pygame.K_w]:

  print("w")

  if m1.y - m1.step > 0:

   m1.y -= m1.step

 elif pygame.key.get_pressed()[pygame.K_s]:

  print("s")

  if m1.y + 100 + m1.step < screenHeight:

   m1.y += m1.step

 if 60 < ball.x - ball.radius < 80:

  if m1.y <= ball.y - ball.radius <= (m1.y + 99):

   ball.x = 95

   ball.stepx *= -1

   dir *= -1

  elif m1.y - 3 <= ball.y - ball.radius <= m2.y + 2:

   ball.x -= 5

   ball.stepy *= -1

  elif m1.y + 98 <= ball.y - ball.radius <= m2.y + 103:

   ball.x -= 5

   ball.stepy *= -1

 if 620 < ball.x + ball.radius < 640:

  if m2.y + 1 <= ball.y + ball.radius <= m2.y + 99:

   ball.x = 605

   ball.stepx *= -1

   dir *= -1

  elif m2.y - 3 <= ball.y + ball.radius <= m2.y + 2:

   ball.x += 5

   ball.stepy *= -1

  elif m2.y + 100 <= ball.y + ball.radius <= m2.y + 105:

   ball.x += 5

   ball.stepy *= -1

 if ball.stepy > 0:

  if ball.y < screenHeight - ball.radius:

   ball.y += ball.stepy

  else:

   ball.y -= 10

   ball.stepy *= -1

 elif ball.stepy < 0:

  if ball.radius < ball.y < screenHeight - ball.radius:

   ball.y += ball.stepy

  else:

   ball.y += 10

   ball.stepy *= -1

 if ball.radius < ball.x < screenWidth - ball.radius:

  ball.x += ball.stepx

 elif ball.x > screenWidth - (ball.radius * 1.5):

  ball.x = ball.start_x

  ball.y = ball.start_y

  dir *= -1

  score1 += 1

  ball.stepy = randint(2, 7)

  ball.stepx = randint(2, 7) * dir

 elif ball.x < 0 + (ball.radius * 1.5):

  ball.x = ball.start_x

  ball.y = ball.start_y

  dir *= -1

  score2 += 1

  ball.stepy = randint(2, 7)

  ball.stepx = randint(2, 7) * dir

 redraw()

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

لا توجد أي إجابات على هذا السؤال بعد

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...