import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Bouncing Red Ball")
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
x = 400
y = 300
center = (x, y)
radius = 80
mnb = 20
bbn = 20
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(black)
keys = pygame.key.get_pressed()
y = y + mnb
if y > 600:
mnb = -3
if y < 0:
mnb = 3
x = x + bbn
if x > 800:
bbn = -3
if x < 0:
bbn = 3
center = (x, y)
pygame.draw.circle(screen, red, center, radius)
pygame.display.flip()
pygame.quit()
sys.exit()
|
import random
import pygame
import math
def dist(x1, y1, x2, y2):
a = (x2-x1) * (x2-x1)
b = (y2-y1) * (y2-y1)
c = a+b
return (math.sqrt(c))
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Snake Remake")
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
x = 400
y = 300
center = (x, y)
radius = 50
kll = 1
mnb = 20
bbn = 20
objx = 100
objy = 100
objcenter = (objx, objy)
objr = 20
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(black)
keys = pygame.key.get_pressed()
if keys[pygame.K_d]:
x = x+kll
if keys[pygame.K_a]:
x = x-kll
if keys[pygame.K_w]:
y = y-kll
if keys[pygame.K_s]:
y = y+kll
if x>800:
x = 400
y = 300
if y>600:
x = 400
y = 300
if x<0:
x = 400
y = 300
if y<0:
x = 400
y = 300
if dist(x, y, objx, objy)800:
radius = 50
kll = kll+1
objcenter = (objx, objy)
pygame.draw.circle(screen, green, objcenter, objr)
center = (x, y)
pygame.draw.circle(screen, red, center, radius)
pygame.display.flip()
pygame.quit()
sys.exit()
|
import pygame
import sys
import time
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Traffic simulation")
score = 0
trafficblack = (0, 0, 0)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
darkgreen = (1, 50, 32)
grey = (128, 128, 128)
yellow = (255, 255, 0)
red1 = red
yellow1 = grey
green1 = grey
ghfj = (0, 0, 0)
red1 = grey
yellow1 = grey
green1 = green
state = 0
state2 = 1
carx = 750
cary = 575
x = 400
y = 300
center = (x, y)
radius = 50
start = time.time()
start2 = time.time()
crossing = False
collision = False
lives = 3
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(darkgreen)
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
cary = cary - 0.8
if cary < -100:
cary = 650
score = score + 1
if not lives <= 0:
if state == 0 and time.time() - start > 5:
state = 1
start = time.time()
elif state == 2 and time.time() - start > 1:
state = 0
start = time.time()
elif state == 1 and time.time() - start > 4:
state = 2
start = time.time()
if state == 0:
red1 = red
yellow1 = grey
green1 = grey
elif state == 1:
red1 = grey
yellow1 = grey
green1 = green
else:
red1 = grey
yellow1 = yellow
green1 = grey
if state2 == 0 and time.time() - start2 > 5:
state2 = 1
start2 = time.time()
elif state2 == 2 and time.time() - start2 > 1:
state2 = 0
start2 = time.time()
elif state2 == 1 and time.time() - start2 > 4:
state2 = 2
start2 = time.time()
if state2 == 0:
red2 = red
yellow2 = grey
green2 = grey
elif state2 == 1:
red2 = grey
yellow2 = grey
green2 = green
else:
red2 = grey
yellow2 = yellow
green2 = grey
center = (x, y)
pygame.draw.circle(screen, red, center, radius)
pygame.draw.rect(screen, grey, pygame.Rect(350, 0, 100, 600))
pygame.draw.rect(screen, grey, pygame.Rect(0, 250, 800, 100))
pygame.draw.rect(screen, trafficblack, pygame.Rect(280, 130, 50, 100))
pygame.draw.rect(screen, trafficblack, pygame.Rect(470, 370, 50, 100))
pygame.draw.circle(screen, red1, (305, 150), 12)
pygame.draw.circle(screen, yellow1, (305, 180), 12)
pygame.draw.circle(screen, green1, (305, 210), 12)
pygame.draw.circle(screen, red2, (495, 390), 12)
pygame.draw.circle(screen, yellow2, (495, 420), 12)
pygame.draw.circle(screen, green2, (495, 450), 12)
cross = pygame.draw.rect(screen, grey, pygame.Rect(350, 250, 100, 100))
if state == 0 and carx > 425 and carx < 450:
pass
else:
carx = carx - 0.8
if carx < -100:
carx = 900
if cary >= 175 and cary <= 325 and carx >= 275 and carx <= 425:
black = (255, 255, 255)
else:
black = (0, 0, 0)
car = pygame.Rect(carx, 275, 100, 50)
pygame.draw.rect(screen, red, car)
car2 = pygame.Rect(375, cary, 50, 100)
pygame.draw.rect(screen, ghfj, car2)
if car.colliderect(car2) and not collision:
lives -= 1
collision = True
elif not car.colliderect(car2):
collision = False
font = pygame.font.Font(None, 36)
text = font.render(f'Lives left: {lives}', True, (255, 255, 255))
screen.blit(text, (10, 10))
font = pygame.font.Font(None, 36)
text = font.render(f'Laps completed: {score}', True, (255, 255, 255))
screen.blit(text, (500, 10))
if state2 == 0 and car2.colliderect(cross) and not crossing:
lives -= 1
crossing = True
ghfj = (255, 255, 255)
elif not car2.colliderect(cross):
crossing = False
ghfj = (0, 0, 0)
else:
font = pygame.font.Font(None, 100)
text = font.render(f'you got: {score} points', True, (155, 30, 30))
screen.blit(text, (100, 250))
pygame.display.flip()
pygame.quit()
sys.exit()
|
import pygame
import sys
import random
import time
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("rythem game")
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
score = 0
yippi = 0
fo = -500
num = [fo, fo, fo, fo]
slo = 0.5
font_size = 200
font = pygame.font.Font(None, font_size)
running = True
start = time.time()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key in (pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4):
val = event.key - pygame.K_1
if num[val] == fo:
score -= 1
else:
num[val] = fo
slo += 0.1
score += 1
screen.fill(black)
if time.time() - start > 1:
n = random.randint(0, 3)
if num[n] == fo:
num[n] = -50
start = time.time()
for i in range(4):
text = font.render(str(i + 1), True, red)
if num[i] != fo:
num[i] += slo
y = num[i]
square = pygame.Rect(i * 200, y, 150, 150)
pygame.draw.rect(screen, white, square)
text_rect = text.get_rect(center=(i * 200 + 75, y + 75))
screen.blit(text, text_rect)
if score >= 10:
slo += 1
score = 0
yippi += 1
if y >= 600:
running = False
font_small = pygame.font.Font(None, 36)
score_text = font_small.render(f'Keys pressed: {score}', True, (255, 255, 255))
screen.blit(score_text, (500, 10))
yips_text = font_small.render(f'score counts by 10: {yippi}', True, (255, 255, 255))
screen.blit(yips_text, (500, 50))
if score <= -1:
running = False
pygame.display.flip()
pygame.quit()
sys.exit()
|