تمرینهای دوره پایتون پیشرفته- سطح 1
پروژه دوره بازی سازی پیشرفته
from turtle import delay
import pygame as pg
import random as rd
import pygame_menu as pgm
def score_value(TxT):
global score
score = SCORE.get_value()
def player_select(PlaYer, SpEEd, LenGTh):
global red_speed, green_speed
global red_length, green_length
red_val = red_player.get_value()
green_val = green_player.get_value()
red_speed = red_val[0][1]
green_speed = green_val[0][1]
red_length = red_val[0][2]
green_length= green_val[0][2]
def set_difficulty(value, difficulty):
global delay
delay = selector.get_value()[0][1]
def start_the_game():
global delay
global red_speed, green_speed
global red_length, green_length
global score
score = int(score)
PLAYER_WIDTH = 10
BALL_WIDTH = 10
x_red , y_red = 10, HEIGHT/2-red_length/2
x_green, y_green = WIDTH-10-PLAYER_WIDTH, HEIGHT/2-green_length/2
x_ball , y_ball = WIDTH/2-BALL_WIDTH/2, HEIGHT/2-BALL_WIDTH/2
x_change = rd.choice([1,-1])
y_change = rd.choice([1,-1])
red_goals = 0
green_goals = 0
game_over = False
RUN = False
run = True
while run:
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_h:
RUN = True
WIN.fill(BLUE)
green = pg.Rect(x_green, y_green, PLAYER_WIDTH, green_length)
red = pg.Rect(x_red, y_red, PLAYER_WIDTH, red_length)
ball = pg.Rect(x_ball, y_ball, BALL_WIDTH, BALL_WIDTH)
pg.draw.rect(WIN, GRAY, [WIDTH/2-2.5, 0, 5, HEIGHT])
pg.draw.rect(WIN, RED, red)
pg.draw.rect(WIN, GREEN, green)
pg.draw.rect(WIN, BLACK, ball)
if red.colliderect(ball):
x_change = 1
if green.colliderect(ball):
x_change = -1
if y_ball <= 0:
y_change = 1
if y_ball >= HEIGHT-BALL_WIDTH:
y_change = -1
if x_ball <= 0:
green_goals += 1
x_ball, y_ball = WIDTH/2-BALL_WIDTH/2, HEIGHT/2-BALL_WIDTH/2
x_change = rd.choice([1,-1])
y_change = rd.choice([1,-1])
RUN = False
if x_ball >= WIDTH-BALL_WIDTH:
red_goals += 1
x_ball, y_ball = WIDTH/2-BALL_WIDTH/2, HEIGHT/2-BALL_WIDTH/2
x_change = rd.choice([1,-1])
y_change = rd.choice([1,-1])
RUN = False
r_goals = font.render(‘score: ‘+str(red_goals), True, GREEN, RED)
g_goals = font.render(‘score: ‘+str(green_goals), True, RED, GREEN)
WIN.blit(r_goals, (WIDTH/4-r_goals.get_width()/2,10))
WIN.blit(g_goals, (3*WIDTH/4-g_goals.get_width()/2,10))
if RUN:
x_ball += x_change
y_ball += y_change
if red_goals >= score:
winner = font.render(‘RED player WINS!’, True, BLUE, RED)
WIN.blit(winner, (WIDTH/2-winner.get_width()/2, HEIGHT/2-winner.get_height()/2))
game_over = True
if green_goals >= score:
winner = font.render(‘GREEN player WINS!’, True, BLUE, GREEN)
WIN.blit(winner, (WIDTH/2-winner.get_width()/2, HEIGHT/2-winner.get_height()/2))
game_over = True
keys = pg.key.get_pressed()
if keys[pg.K_UP] and y_green > 0 and RUN:
y_green -= green_speed
if keys[pg.K_DOWN] and y_green < HEIGHT – green_length and RUN:
y_green += green_speed
if keys[pg.K_w] and y_red > 0 and RUN:
y_red -= red_speed
if keys[pg.K_s] and y_red < HEIGHT – red_length and RUN:
y_red += red_speed
pg.display.update()
pg.time.delay(delay)
if game_over:
run = False
pg.time.delay(3000)
BLUE = (90,90,200)
RED = (200,90,90)
GREEN= (90,200,90)
GRAY = (150,150,150)
BLACK= (0,0,0)
white= (225,225,225)
pg.init()
font = pg.font.Font(‘freesansbold.ttf’, 32)
WIDTH, HEIGHT = 800, 480
WIN = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption(“pong!”)
menu
maryam = int(input(“enter your work.1=+/2=-/3×/۴=÷”))
if maryam == 1:
A = int(input(“enter your first number”))
B = int(input(“enter your second number”))
print (A+B)
elif maryam == 2:
C = int(input(“enter your first number”))
D = int(input(“enter your second number”))
print (C-D)
elif maryam == 3:
a = int(input(“enter your first number”))
b = int(input(“enter your second number”))
print (a*b)
elif maryam == 4:
c = int(input(“enter your first number”))
d = int(input(“enter your second number”))
print (c/d)
else:
print(“i am so soory.you should enter a number between 1 to 4.”)
# soal 1:
print(“1+1?”)
a1 =int(input(“your answer:”))
if a1 == 2:
maryam = 1
else:
maryam = 0
# soal 2:
print(“1+2?”)
a2 =int(input(“your answer:”))
if a2 == 3:
maryam += 1
else:
maryam += 0
# soal 3:
print(“1+3?”)
a3 =int(input(“your answer:”))
if a3 == 4:
maryam += 1
else:
maryam += 0
# nomre va ina!:
print(maryam)
print(“from 3”)
if maryam == 0:
print(“not good at all!”)
elif maryam == 1:
print(“not bad”)
elif maryam == 2:
print(“good…”)
elif maryam == 3:
print(“perfect!”)