Python

Python смотрите люди

tunnel_list = [x-speed if x-speed>-200 else 2100 for x in tunnel_list ]


def draw_tunnel():

for x in tunnel_list:

pygame.draw.rect(screen,COLORS['darkgreen'],(x,0,100,350),0)

pygame.draw.rect(screen,COLORS['darkgreen'],(x+100,550,100,350),0)

G = 9.8*30 # g

JUMP_V = -300

bird_x,bird_y = 700,450

bird_v = 0


if not jump:

bird_v += G*frame

else:

bird_v = JUMP_V

jump = False

bird_y += frame*bird_v


def draw_bird():

screen.blit(birdImg,[bird_x,bird_y])

def rect_cover(rect1,rect2,up=True):

# bird

left_up1 = (rect1[0],rect1[1])

left_down1 = (rect1[0],left_up1[1]+rect1[3])

right_up1 = (left_up1[0]+rect1[2],rect1[1])

right_down1 = (left_up1[0]+rect1[2],left_up1[1]+rect1[3])

# tunnel

left_up2 = (rect2[0],rect2[1])

left_down2 = (rect2[0],left_up2[1]+rect2[3])

right_up2 = (left_up2[0]+rect2[2],rect2[1])

right_down2 = (left_up2[0]+rect2[2],left_up2[1]+rect2[3])

# check

if (left_up2 [0] <= right_up1 [0] <= right_up2 [0]): # x, это должен быть правый контакт линии, чтобы вы могли судить правую часть птицы

if up and (left_up2[1]<=right_up1[1]<=left_down2[1]):

return True

elif (not up) and (left_up2[1]<=right_down1[1]<=left_down2[1]):

return True

return False

def draw_dead():

s = pygame.Surface(SIZE, pygame.SRCALPHA)

s.fill((255,255,255,240))

screen.blit(s, (0,0))

txt = font120.render('YOU DEAD',True,COLORS['black'])

x,y = 450,400

screen.blit(txt,(x,y))

import sys


import pygame

from pygame.color import THECOLORS as COLORS


def draw_background():

# white background

screen.fill(COLORS['lightblue'])

pygame.draw.rect(screen,COLORS['black'],(-100,902,3000,200),5)


def draw_tunnel():

for x in tunnel_list:

pygame.draw.rect(screen,COLORS['darkgreen'],(x,0,100,350),0)

pygame.draw.rect(screen,COLORS['darkgreen'],(x+100,550,100,350),0)


def draw_bird():

screen.blit(birdImg,[bird_x,bird_y])


def draw_context():

txt = font50.render('Count time: '+str(int(count_time))+' S',True,COLORS['black'])

x,y = 10,920

screen.blit(txt,(x,y))


def draw_pause():

s = pygame.Surface(SIZE, pygame.SRCALPHA)

s.fill((255,255,255,220))

screen.blit(s, (0,0))

txt = font120.render('PAUSE',True,COLORS['darkgray'])

x,y = 550,400

screen.blit(txt,(x,y))


def draw_dead():

s = pygame.Surface(SIZE, pygame.SRCALPHA)

s.fill((255,255,255,240))

screen.blit(s, (0,0))

txt = font120.render('YOU DEAD',True,COLORS['black'])

x,y = 450,400

screen.blit(txt,(x,y))


def rect_cover(rect1,rect2,up=True):

# bird

left_up1 = (rect1[0],rect1[1])

left_down1 = (rect1[0],left_up1[1]+rect1[3])

right_up1 = (left_up1[0]+rect1[2],rect1[1])

right_down1 = (left_up1[0]+rect1[2],left_up1[1]+rect1[3])

# tunnel

left_up2 = (rect2[0],rect2[1])

left_down2 = (rect2[0],left_up2[1]+rect2[3])

right_up2 = (left_up2[0]+rect2[2],rect2[1])

right_down2 = (left_up2[0]+rect2[2],left_up2[1]+rect2[3])

# check

if (left_up2 [0] <= right_up1 [0] <= right_up2 [0]): # x, это должен быть правый контакт линии, чтобы вы могли судить правую часть птицы

if up and (left_up2[1]<=right_up1[1]<=left_down2[1]):

return True

elif (not up) and (left_up2[1]<=right_down1[1]<=left_down2[1]):

return True

return False
И на что смотреть? Pygame это не такой уж прям и Python, там под капотом на самом деле прячется CИ
РМ
Роман Михеев
68 723
Лучший ответ
Ошибка была везде!
import turtle
import random

w = 500
h = 500
food_size = 10
delay = 100

offsets = {
"up": (0, 20),
"down": (0, -20),
"left": (-20, 0),
"right": (20, 0)
}


def reset():
global snake, snake_dir, food_position, pen
snake = [[0, 0], [0, 20], [0, 40], [0, 60], [0, 80]]
snake_dir = "up"
food_position = get_random_food_position()
food.goto(food_position)
move_snake()


def move_snake():
global snake_dir

new_head = snake[-1].copy()
new_head[0] = snake[-1][0] + offsets[snake_dir][0]
new_head[1] = snake[-1][1] + offsets[snake_dir][1]

if new_head in snake[:-1]:
reset()
else:
snake.append(new_head)

if not food_collision():
snake.pop(0)

if snake[-1][0] > w / 2:
snake[-1][0] -= w
elif snake[-1][0] < - w / 2:
snake[-1][0] += w
elif snake[-1][1] > h / 2:
snake[-1][1] -= h
elif snake[-1][1] < -h / 2:
snake[-1][1] += h

pen.clearstamps()

for segment in snake:
pen.goto(segment[0], segment[1])
pen.stamp()

screen.update()

turtle.ontimer(move_snake, delay)


def food_collision():
global food_position
if get_distance(snake[-1], food_position) < 20:
food_position = get_random_food_position()
food.goto(food_position)
return True
return False


def get_random_food_position():
x = random.randint(- w / 2 + food_size, w / 2 - food_size)
y = random.randint(- h / 2 + food_size, h / 2 - food_size)
return (x, y)


def get_distance(pos1, pos2):
x1, y1 = pos1
x2, y2 = pos2
distance = ((y2 - y1) ** 2 + (x2 - x1) ** 2) ** 0.5
return distance


def go_up():
global snake_dir
if snake_dir != "down":
snake_dir = "up"


def go_right():
global snake_dir
if snake_dir != "left":
snake_dir = "right"


def go_down():
global snake_dir
if snake_dir!= "up":
snake_dir = "down"


def go_left():
global snake_dir
if snake_dir != "right":
snake_dir = "left"


screen = turtle.Screen()
screen.setup(w, h)
screen.title("Snake")
screen.bgcolor("blue")
screen.setup(500, 500)
screen.tracer(0)


pen = turtle.Turtle("square")
pen.penup()


food = turtle.Turtle()
food.shape("square")
food.color("yellow")
food.shapesize(food_size / 20)
food.penup()


screen.listen()
screen.onkey(go_up, "Up")
screen.onkey(go_right, "Right")
screen.onkey(go_down, "Down")
screen.onkey(go_left, "Left")


reset()
turtle.done()
Смольский
Смольский
6 952