前面小编已经出了小游戏~表白代码~画一朵樱花系列~
今天就出个画图跟游戏的合集叭~༼ つ ◕_◕ ༽つ
喜欢记得给小编三连噢~~~
一、画图系列1、画佩奇
预览图:
源码:
# coding: utf-8
import turtle as t
t.screensize(400, 300)
t.pensize(4) # 设置画笔的大小
t.colormode(255) # 设置GBK颜色范围为0-255
t.color((255, 155, 192), "pink") # 设置画笔颜色和填充颜色(pink)
t.setup(840, 500) # 设置主窗口的大小为840*500
t.speed(10) # 设置画笔速度为10
# 鼻子
t.pu() # 提笔
t.goto(-100, 100) # 画笔前往坐标(-100,100)
t.pd() # 下笔
t.seth(-30) # 笔的角度为-30°
t.begin_fill() # 外形填充的开始标志
a = 0.4
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.08
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.08
t.lt(3)
t.fd(a)
t.end_fill() # 依据轮廓填充
t.pu() # 提笔
t.seth(90) # 笔的角度为90度
t.fd(25) # 向前移动25
t.seth(0) # 转换画笔的角度为0
t.fd(10)
t.pd()
t.pencolor(255, 155, 192) # 设置画笔颜色
t.seth(10)
t.begin_fill()
t.circle(5) # 画一个半径为5的圆
t.color(160, 82, 45) # 设置画笔和填充颜色
t.end_fill()
t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()
# 头
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pd()
t.begin_fill()
t.seth(180)
t.circle(300, -30) # 顺时针画一个半径为300,圆心角为30°的园
t.circle(100, -60)
t.circle(80, -100)
t.circle(150, -20)
t.circle(60, -95)
t.seth(161)
t.circle(-300, 15)
t.pu()
t.goto(-100, 100)
t.pd()
t.seth(-30)
a = 0.4
for i in range(60):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.08
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.08
t.lt(3)
t.fd(a)
t.end_fill()
# 耳朵
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 54)
t.end_fill()
t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 56)
t.end_fill()
# 眼睛
t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
# 腮
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill()
# 嘴
t.color(239, 69, 19)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.seth(-80)
t.circle(30, 40)
t.circle(40, 80)
# 身体
t.color("red", (255, 99, 71))
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pd()
t.begin_fill()
t.seth(-130)
t.circle(100, 10)
t.circle(300, 30)
t.seth(0)
t.fd(230)
t.seth(90)
t.circle(300, 30)
t.circle(100, 3)
t.color((255, 155, 192), (255, 100, 100))
t.seth(-135)
t.circle(-80, 63)
t.circle(-150, 24)
t.end_fill()
# 手
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pd()
t.seth(-160)
t.circle(300, 15)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-10)
t.circle(-20, 90)
t.pu()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pd()
t.seth(-20)
t.circle(-300, 15)
t.pu()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-170)
t.circle(20, 90)
# 脚
t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
# 尾巴
t.pensize(4)
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pd()
t.seth(0)
t.circle(70, 20)
t.circle(10, 330)
t.circle(70, 30)
t.done()
2、画一朵玫瑰
预览图:
源码:
import turtle
import time
turtle.speed(5) #画笔移动的速度
# 设置初始位置
turtle.penup() #提起画笔,移动画笔但并不会绘制图形
turtle.left(90) #逆时针转动画笔90度
turtle.fd(200)
turtle.pendown() #放下画笔,移动画笔即开始绘制
turtle.right(90)
#设置画笔的大小
turtle.pensize(2)
# 花蕊
turtle.fillcolor("red") #填充颜色
turtle.begin_fill() #开始填充
turtle.circle(10,180)
turtle.circle(25,110)
turtle.left(50)
turtle.circle(60,45)
turtle.circle(20,170)
turtle.right(24)
turtle.fd(30)
turtle.left(10)
turtle.circle(30,110)
turtle.fd(20)
turtle.left(40)
turtle.circle(90,70)
turtle.circle(30,150)
turtle.right(30)
turtle.fd(15)
turtle.circle(80,90)
turtle.left(15)
turtle.fd(45)
turtle.right(165)
turtle.fd(20)
turtle.left(155)
turtle.circle(150,80)
turtle.left(50)
turtle.circle(150,90)
turtle.end_fill() #结束填充
# 花瓣1
turtle.left(150)
turtle.circle(-90,70)
turtle.left(20)
turtle.circle(75,105)
turtle.setheading(60)
turtle.circle(80,98)
turtle.circle(-90,40)
# 花瓣2
turtle.left(180)
turtle.circle(90,40)
turtle.circle(-80,98)
turtle.setheading(-83)
# 叶子1
turtle.fd(30)
turtle.left(90)
turtle.fd(25)
turtle.left(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(-80,90)
turtle.right(90)
turtle.circle(-80,90)
turtle.end_fill()
turtle.right(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(85)
turtle.left(90)
turtle.fd(80)
# 叶子2
turtle.right(90)
turtle.right(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(80,90)
turtle.left(90)
turtle.circle(80,90)
turtle.end_fill()
turtle.left(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(60)
turtle.right(90)
turtle.circle(200,50) #画一个圆 200 是半径,50 是弧度
#不让自动退出,放在程序的最后一行
#不然画画结束后会自动退出
turtle.done()
3、画个蓝胖子
预览图:
二、小游戏系列1、超级玛丽
预览图:
源码:
__author__ = 'marble_xu'
import os
import json
import pygame as pg
from .. import setup, tools
from .. import constants as c
from ..components import powerup
class Player(pg.sprite.Sprite):
def __init__(self, player_name):
pg.sprite.Sprite.__init__(self)
self.player_name = player_name
self.load_data()
self.setup_timer()
self.setup_state()
self.setup_speed()
self.load_images()
if c.DEBUG:
self.right_frames = self.big_fire_frames[0]
self.left_frames = self.big_fire_frames[1]
self.big = True
self.fire = True
self.frame_index = 0
self.state = c.WALK
self.image = self.right_frames[self.frame_index]
self.rect = self.image.get_rect()
def restart(self):
'''restart after player is dead or go to next level'''
if self.dead:
self.dead = False
self.big = False
self.fire = False
self.set_player_image(self.small_normal_frames, 0)
self.right_frames = self.small_normal_frames[0]
self.left_frames = self.small_normal_frames[1]
self.state = c.STAND
def load_data(self):
player_file = str(self.player_name) + '.json'
file_path = os.path.join('source', 'data', 'player', player_file)
f = open(file_path)
self.player_data = json.load(f)
def setup_timer(self):
self.walking_timer = 0
self.death_timer = 0
self.flagpole_timer = 0
self.transition_timer = 0
self.hurt_invincible_timer = 0
self.invincible_timer = 0
self.last_fireball_time = 0
def setup_state(self):
self.facing_right = True
self.allow_jump = True
self.allow_fireball = True
self.dead = False
self.big = False
self.fire = False
self.hurt_invincible = False
self.invincible = False
self.crouching = False
def setup_speed(self):
speed = self.player_data[c.PLAYER_SPEED]
self.x_vel = 0
self.y_vel = 0
self.max_walk_vel = speed[c.MAX_WALK_SPEED]
self.max_run_vel = speed[c.MAX_RUN_SPEED]
self.max_y_vel = speed[c.MAX_Y_VEL]
self.walk_accel = speed[c.WALK_ACCEL]
self.run_accel = speed[c.RUN_ACCEL]
self.jump_vel = speed[c.JUMP_VEL]
self.gravity = c.GRAVITY
self.max_x_vel = self.max_walk_vel
self.x_accel = self.walk_accel
def load_images(self):
sheet = setup.GFX['mario_bros']
frames_list = self.player_data[c.PLAYER_frameS]
self.right_frames = []
self.left_frames = []
self.right_small_normal_frames = []
self.left_small_normal_frames = []
self.right_big_normal_frames = []
self.left_big_normal_frames = []
self.right_big_fire_frames = []
self.left_big_fire_frames = []
for name, frames in frames_list.items():
for frame in frames:
image = tools.get_image(sheet, frame['x'], frame['y'],
frame['width'], frame['height'],
c.BLACK, c.SIZE_MULTIPLIER)
left_image = pg.transform.flip(image, True, False)
if name == c.RIGHT_SMALL_NORMAL:
self.right_small_normal_frames.append(image)
self.left_small_normal_frames.append(left_image)
elif name == c.RIGHT_BIG_NORMAL:
self.right_big_normal_frames.append(image)
self.left_big_normal_frames.append(left_image)
elif name == c.RIGHT_BIG_FIRE:
self.right_big_fire_frames.append(image)
self.left_big_fire_frames.append(left_image)
self.small_normal_frames = [self.right_small_normal_frames,
self.left_small_normal_frames]
self.big_normal_frames = [self.right_big_normal_frames,
self.left_big_normal_frames]
self.big_fire_frames = [self.right_big_fire_frames,
self.left_big_fire_frames]
self.all_images = [self.right_small_normal_frames,
self.left_small_normal_frames,
self.right_big_normal_frames,
self.left_big_normal_frames,
self.right_big_fire_frames,
self.left_big_fire_frames]
self.right_frames = self.small_normal_frames[0]
self.left_frames = self.small_normal_frames[1]
def update(self, keys, game_info, fire_group):
self.current_time = game_info[c.CURRENT_TIME]
self.handle_state(keys, fire_group)
self.check_if_hurt_invincible()
self.check_if_invincible()
self.animation()
def handle_state(self, keys, fire_group):
if self.state == c.STAND:
self.standing(keys, fire_group)
elif self.state == c.WALK:
self.walking(keys, fire_group)
elif self.state == c.JUMP:
self.jumping(keys, fire_group)
elif self.state == c.FALL:
self.falling(keys, fire_group)
elif self.state == c.DEATH_JUMP:
self.jumping_to_death()
elif self.state == c.FLAGPOLE:
self.flag_pole_sliding()
elif self.state == c.WALK_AUTO:
self.walking_auto()
elif self.state == c.END_OF_LEVEL_FALL:
self.y_vel += self.gravity
elif self.state == c.IN_CASTLE:
self.frame_index = 0
elif self.state == c.SMALL_TO_BIG:
self.changing_to_big()
elif self.state == c.BIG_TO_SMALL:
self.changing_to_small()
elif self.state == c.BIG_TO_FIRE:
self.changing_to_fire()
elif self.state == c.DOWN_TO_PIPE:
self.y_vel = 1
self.rect.y += self.y_vel
elif self.state == c.UP_OUT_PIPE:
self.y_vel = -1
self.rect.y += self.y_vel
if self.rect.bottom < self.up_pipe_y:
self.state = c.STAND
def check_to_allow_jump(self, keys):
if not keys[tools.keybinding['jump']]:
self.allow_jump = True
def check_to_allow_fireball(self, keys):
if not keys[tools.keybinding['action']]:
self.allow_fireball = True
def standing(self, keys, fire_group):
self.check_to_allow_jump(keys)
self.check_to_allow_fireball(keys)
self.frame_index = 0
self.x_vel = 0
self.y_vel = 0
if keys[tools.keybinding['action']]:
if self.fire and self.allow_fireball:
self.shoot_fireball(fire_group)
if keys[tools.keybinding['down']]:
self.update_crouch_or_not(True)
if keys[tools.keybinding['left']]:
self.facing_right = False
self.update_crouch_or_not()
self.state = c.WALK
elif keys[tools.keybinding['right']]:
self.facing_right = True
self.update_crouch_or_not()
self.state = c.WALK
elif keys[tools.keybinding['jump']]:
if self.allow_jump:
self.state = c.JUMP
self.y_vel = self.jump_vel
if not keys[tools.keybinding['down']]:
self.update_crouch_or_not()
def update_crouch_or_not(self, isDown=False):
if not self.big:
self.crouching = True if isDown else False
return
if not isDown and not self.crouching:
return
self.crouching = True if isDown else False
frame_index = 7 if isDown else 0
bottom = self.rect.bottom
left = self.rect.x
if self.facing_right:
self.image = self.right_frames[frame_index]
else:
self.image = self.left_frames[frame_index]
self.rect = self.image.get_rect()
self.rect.bottom = bottom
self.rect.x = left
self.frame_index = frame_index
def walking(self, keys, fire_group):
self.check_to_allow_jump(keys)
self.check_to_allow_fireball(keys)
if self.frame_index == 0:
self.frame_index += 1
self.walking_timer = self.current_time
elif (self.current_time - self.walking_timer >
self.calculate_animation_speed()):
if self.frame_index < 3:
self.frame_index += 1
else:
self.frame_index = 1
self.walking_timer = self.current_time
if keys[tools.keybinding['action']]:
self.max_x_vel = self.max_run_vel
self.x_accel = self.run_accel
if self.fire and self.allow_fireball:
self.shoot_fireball(fire_group)
else:
self.max_x_vel = self.max_walk_vel
self.x_accel = self.walk_accel
if keys[tools.keybinding['jump']]:
if self.allow_jump:
self.state = c.JUMP
if abs(self.x_vel) > 4:
self.y_vel = self.jump_vel - .5
else:
self.y_vel = self.jump_vel
if keys[tools.keybinding['left']]:
self.facing_right = False
if self.x_vel > 0:
self.frame_index = 5
self.x_accel = c.SMALL_TURNAROUND
self.x_vel = self.cal_vel(self.x_vel, self.max_x_vel, self.x_accel, True)
elif keys[tools.keybinding['right']]:
self.facing_right = True
if self.x_vel < 0:
self.frame_index = 5
self.x_accel = c.SMALL_TURNAROUND
self.x_vel = self.cal_vel(self.x_vel, self.max_x_vel, self.x_accel)
else:
if self.facing_right:
if self.x_vel > 0:
self.x_vel -= self.x_accel
else:
self.x_vel = 0
self.state = c.STAND
else:
if self.x_vel < 0:
self.x_vel += self.x_accel
else:
self.x_vel = 0
self.state = c.STAND
def jumping(self, keys, fire_group):
""" y_vel value: positive is down, negative is up """
self.check_to_allow_fireball(keys)
self.allow_jump = False
self.frame_index = 4
self.gravity = c.JUMP_GRAVITY
self.y_vel += self.gravity
if self.y_vel >= 0 and self.y_vel < self.max_y_vel:
self.gravity = c.GRAVITY
self.state = c.FALL
if keys[tools.keybinding['right']]:
self.x_vel = self.cal_vel(self.x_vel, self.max_x_vel, self.x_accel)
elif keys[tools.keybinding['left']]:
self.x_vel = self.cal_vel(self.x_vel, self.max_x_vel, self.x_accel, True)
if not keys[tools.keybinding['jump']]:
self.gravity = c.GRAVITY
self.state = c.FALL
if keys[tools.keybinding['action']]:
if self.fire and self.allow_fireball:
self.shoot_fireball(fire_group)
def falling(self, keys, fire_group):
self.check_to_allow_fireball(keys)
self.y_vel = self.cal_vel(self.y_vel, self.max_y_vel, self.gravity)
if keys[tools.keybinding['right']]:
self.x_vel = self.cal_vel(self.x_vel, self.max_x_vel, self.x_accel)
elif keys[tools.keybinding['left']]:
self.x_vel = self.cal_vel(self.x_vel, self.max_x_vel, self.x_accel, True)
if keys[tools.keybinding['action']]:
if self.fire and self.allow_fireball:
self.shoot_fireball(fire_group)
def jumping_to_death(self):
if self.death_timer == 0:
self.death_timer = self.current_time
elif (self.current_time - self.death_timer) > 500:
self.rect.y += self.y_vel
self.y_vel += self.gravity
def cal_vel(self, vel, max_vel, accel, isNegative=False):
""" max_vel and accel must > 0 """
if isNegative:
new_vel = vel * -1
else:
new_vel = vel
if (new_vel + accel) < max_vel:
new_vel += accel
else:
new_vel = max_vel
if isNegative:
return new_vel * -1
else:
return new_vel
def calculate_animation_speed(self):
if self.x_vel == 0:
animation_speed = 130
elif self.x_vel > 0:
animation_speed = 130 - (self.x_vel * 13)
else:
animation_speed = 130 - (self.x_vel * 13 * -1)
return animation_speed
def shoot_fireball(self, powerup_group):
if (self.current_time - self.last_fireball_time) > 500:
self.allow_fireball = False
powerup_group.add(powerup.FireBall(self.rect.right,
self.rect.y, self.facing_right))
self.last_fireball_time = self.current_time
self.frame_index = 6
def flag_pole_sliding(self):
self.state = c.FLAGPOLE
self.x_vel = 0
self.y_vel = 5
if self.flagpole_timer == 0:
self.flagpole_timer = self.current_time
elif self.rect.bottom < 493:
if (self.current_time - self.flagpole_timer) < 65:
self.frame_index = 9
elif (self.current_time - self.flagpole_timer) < 130:
self.frame_index = 10
else:
self.flagpole_timer = self.current_time
elif self.rect.bottom >= 493:
self.frame_index = 10
def walking_auto(self):
self.max_x_vel = 5
self.x_accel = self.walk_accel
self.x_vel = self.cal_vel(self.x_vel, self.max_x_vel, self.x_accel)
if (self.walking_timer == 0 or (self.current_time - self.walking_timer) > 200):
self.walking_timer = self.current_time
elif (self.current_time - self.walking_timer >
self.calculate_animation_speed()):
if self.frame_index < 3:
self.frame_index += 1
else:
self.frame_index = 1
self.walking_timer = self.current_time
def changing_to_big(self):
timer_list = [135, 200, 365, 430, 495, 560, 625, 690, 755, 820, 885]
# size value 0:small, 1:middle, 2:big
size_list = [1, 0, 1, 0, 1, 2, 0, 1, 2, 0, 2]
frames = [(self.small_normal_frames, 0), (self.small_normal_frames, 7),
(self.big_normal_frames, 0)]
if self.transition_timer == 0:
self.big = True
self.change_index = 0
self.transition_timer = self.current_time
elif (self.current_time - self.transition_timer) > timer_list[self.change_index]:
if (self.change_index + 1) >= len(timer_list):
# player becomes big
self.transition_timer = 0
self.set_player_image(self.big_normal_frames, 0)
self.state = c.WALK
self.right_frames = self.right_big_normal_frames
self.left_frames = self.left_big_normal_frames
else:
frame, frame_index = frames[size_list[self.change_index]]
self.set_player_image(frame, frame_index)
self.change_index += 1
def changing_to_small(self):
timer_list = [265, 330, 395, 460, 525, 590, 655, 720, 785, 850, 915]
# size value 0:big, 1:middle, 2:small
size_list = [0, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
frames = [(self.big_normal_frames, 4), (self.big_normal_frames, 8),
(self.small_normal_frames, 8)]
if self.transition_timer == 0:
self.change_index = 0
self.transition_timer = self.current_time
elif (self.current_time - self.transition_timer) > timer_list[self.change_index]:
if (self.change_index + 1) >= len(timer_list):
# player becomes small
self.transition_timer = 0
self.set_player_image(self.small_normal_frames, 0)
self.state = c.WALK
self.big = False
self.fire = False
self.hurt_invincible = True
self.right_frames = self.right_small_normal_frames
self.left_frames = self.left_small_normal_frames
else:
frame, frame_index = frames[size_list[self.change_index]]
self.set_player_image(frame, frame_index)
self.change_index += 1
def changing_to_fire(self):
timer_list = [65, 195, 260, 325, 390, 455, 520, 585, 650, 715, 780, 845, 910, 975]
# size value 0:fire, 1:big green, 2:big red, 3:big black
size_list = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1]
frames = [(self.big_fire_frames, 3), (self.big_normal_frames, 3),
(self.big_fire_frames, 3), (self.big_normal_frames, 3)]
if self.transition_timer == 0:
self.change_index = 0
self.transition_timer = self.current_time
elif (self.current_time - self.transition_timer) > timer_list[self.change_index]:
if (self.change_index + 1) >= len(timer_list):
# player becomes fire
self.transition_timer = 0
self.set_player_image(self.big_fire_frames, 3)
self.fire = True
self.state = c.WALK
self.right_frames = self.right_big_fire_frames
self.left_frames = self.left_big_fire_frames
else:
frame, frame_index = frames[size_list[self.change_index]]
self.set_player_image(frame, frame_index)
self.change_index += 1
def set_player_image(self, frames, frame_index):
self.frame_index = frame_index
if self.facing_right:
self.right_frames = frames[0]
self.image = frames[0][frame_index]
else:
self.left_frames = frames[1]
self.image = frames[1][frame_index]
bottom = self.rect.bottom
centerx = self.rect.centerx
self.rect = self.image.get_rect()
self.rect.bottom = bottom
self.rect.centerx = centerx
def check_if_hurt_invincible(self):
if self.hurt_invincible:
if self.hurt_invincible_timer == 0:
self.hurt_invincible_timer = self.current_time
self.hurt_invincible_timer2 = self.current_time
elif (self.current_time - self.hurt_invincible_timer) < 2000:
if (self.current_time - self.hurt_invincible_timer2) < 35:
self.image.set_alpha(0)
elif (self.current_time - self.hurt_invincible_timer2) < 70:
self.image.set_alpha(255)
self.hurt_invincible_timer2 = self.current_time
else:
self.hurt_invincible = False
self.hurt_invincible_timer = 0
for frames in self.all_images:
for image in frames:
image.set_alpha(255)
def check_if_invincible(self):
if self.invincible:
if self.invincible_timer == 0:
self.invincible_timer = self.current_time
self.invincible_timer2 = self.current_time
elif (self.current_time - self.invincible_timer) < 10000:
if (self.current_time - self.invincible_timer2) < 35:
self.image.set_alpha(0)
elif (self.current_time - self.invincible_timer2) < 70:
self.image.set_alpha(255)
self.invincible_timer2 = self.current_time
elif (self.current_time - self.invincible_timer) < 12000:
if (self.current_time - self.invincible_timer2) < 100:
self.image.set_alpha(0)
elif (self.current_time - self.invincible_timer2) < 200:
self.image.set_alpha(255)
self.invincible_timer2 = self.current_time
else:
self.invincible = False
self.invincible_timer = 0
for frames in self.all_images:
for image in frames:
image.set_alpha(255)
def animation(self):
if self.facing_right:
self.image = self.right_frames[self.frame_index]
else:
self.image = self.left_frames[self.frame_index]
def start_death_jump(self, game_info):
self.dead = True
self.y_vel = -11
self.gravity = .5
self.frame_index = 6
self.state = c.DEATH_JUMP
2、俄罗斯方块
预览图
源码:
#_*_ coding:utf-8 _*_
from tkinter import *
import random
import time
import tkinter.messagebox
#俄罗斯方块界面的高度
HEIGHT = 20
#俄罗斯方块界面的宽度
WIDTH = 10
ACTIVE = 1
PASSIVE = 0
TRUE = 1
FALSE = 0
style = [
[[(0,0),(0,1),(1,1),(2,1)],[(1,0),(1,1),(1,2),(0,2)],[(0,1),(1,1),(2,1),(2,2)],[(1,0),(2,0),(1,1),(1,2)]],#j
[[(1,0),(1,1),(1,2),(2,1)],[(1,0),(0,1),(1,1),(2,1)],[(1,0),(1,1),(1,2),(0,1)],[(0,1),(1,1),(2,1),(1,2)]],#T
[[(0,1),(1,1),(2,1),(2,0)],[(0,0),(1,0),(1,1),(1,2)],[(0,1),(1,1),(2,1),(0,2)],[(1,0),(1,1),(1,2),(2,2)]],#反L
[[(0,0),(0,1),(1,1),(1,2)],[(2,1),(1,1),(1,2),(0,2)],[(0,0),(0,1),(1,1),(1,2)],[(2,1),(1,1),(1,2),(0,2)]],#Z
[[(1,0),(1,1),(0,1),(0,2)],[(0,1),(1,1),(1,2),(2,2)],[(1,0),(1,1),(0,1),(0,2)],[(0,1),(1,1),(1,2),(2,2)]],#反Z
[[(0,0),(0,1),(1,1),(1,0)],[(0,0),(0,1),(1,1),(1,0)],[(0,0),(0,1),(1,1),(1,0)],[(0,0),(0,1),(1,1),(1,0)]],#田
[[(1,0),(1,1),(1,2),(1,3)],[(0,1),(1,1),(2,1),(3,1)],[(1,0),(1,1),(1,2),(1,3)],[(0,1),(1,1),(2,1),(3,1)]]#长条
]
root=Tk();
root.title('俄罗斯方块')
class App(frame):
def __init__(self,master):
frame.__init__(self)
master.bind('',self.Up)
master.bind('',self.Left)
master.bind('',self.Right)
master.bind('',self.Down)
master.bind('',self.Space)
master.bind('',self.Play)
master.bind('',self.Pause)
master.bind('',self.StartByS)
# rgb颜色值
self.backg="#%02x%02x%02x" % (120,150,30) #大背景
self.frontg="#%02x%02x%02x" % (40,120,150) #下一个形状颜色
self.nextg="#%02x%02x%02x" % (150,100,100) #小背景
self.flashg="#%02x%02x%02x" % (210,130,100) #炸的颜色
self.LineDisplay=Label(master,text='Lines: ',bg='black',fg='red')
self.Line=Label(master,text='0',bg='black',fg='red')
self.ScoreDisplay=Label(master,text='Score: ',bg='black',fg='red')
self.Score=Label(master,text='0',bg='black',fg='red')
self.SpendTimeDisplay=Label(master,text='Time: ',bg='black',fg='red')
self.SpendTime=Label(master,text='0.0',bg='black',fg='red')
self.LineDisplay.grid(row=HEIGHT-2,column=WIDTH,columnspan=2)
self.Line.grid(row=HEIGHT-2,column=WIDTH+2,columnspan=3)
self.ScoreDisplay.grid(row=HEIGHT-1,column=WIDTH,columnspan=2)
self.Score.grid(row=HEIGHT-1,column=WIDTH+2,columnspan=3)
self.SpendTimeDisplay.grid(row=HEIGHT-4,column=WIDTH,columnspan=2)
self.SpendTime.grid(row=HEIGHT-4,column=WIDTH+2,columnspan=3)
self.TotalTime=0.0
self.TotalLine=0
self.TotalScore=0
#游戏结束
self.isgameover=FALSE
#暂停
self.isPause=FALSE
#开始
self.isStart=FALSE
self.NextList=[] #整个小背景
self.NextRowList=[] #一行小背景
self.px=0
self.py=0 #记录方块参考点
#渲染小背景
r=0;c=0
for k in range(4*4):
LN=Label(master,text=' ',bg=str(self.nextg),fg='white',relief=FLAT,bd=3)
LN.grid(row=r,column=WIDTH+c,sticky=N+E+S+W)
self.NextRowList.append(LN)
c=c+1
if c>=4:
r=r+1;c=0
self.NextList.append(self.NextRowList)
self.NextRowList=[]
#渲染大背景
self.BlockList=[]
self.BlockRowList=[]
self.LabelList=[]
self.LabelRowList=[]
row=0;col=0
for i in range(HEIGHT*WIDTH):
L=Label(master,text=' ',bg=str(self.backg),fg='white',relief=FLAT,bd=4)
L.grid(row=row,column=col,sticky=N+E+S+W)
L.row=row;L.col=col;L.isactive=PASSIVE
self.BlockRowList.append(0); #大背景每个格子初始化为0值
self.LabelRowList.append(L)
col=col+1
if col>=WIDTH:
row=row+1;col=0
self.BlockList.append(self.BlockRowList)
self.LabelList.append(self.LabelRowList)
self.BlockRowList=[]
self.LabelRowList=[]
#file
fw=open('text.txt','a')
fw.close()
hasHead=FALSE
f=open('text.txt','r')
if f.read(5)=='score':
hasHead=TRUE
f.close()
self.file=open('text.txt','a')
if hasHead==FALSE:
self.file.write('score line time scorePtime linePtime scorePline date/n')
self.file.flush()
self.time=1000
self.onTimer()
def __del__(self):
#self.file.close()
pass
def Pause(self,event):
self.isPause=1-self.isPause
def Up(self,event):
BL=self.BlockList #格子的值
LL=self.LabelList #格子Label
Moveable=TRUE #是否可旋转
#代码编写开始
nowStyle = style[self.xnow][(self.ynow)]
newStyle = style[self.xnow][(self.ynow+1)%4] #算出下一俄罗斯方块
self.ynow = (self.ynow+1)%4 #此行代码非常重要,否则响应UP时,只能变第一次
print("nowStyle:"+str(nowStyle)+"=====>>newStyle:"+str(newStyle))
#根据现有形状中每个label的坐标计算出旋转后目标坐标(x,y)
SourceList=[];DestList=[]
for i in range(4):
SourceList.append([ nowStyle[i][0]+self.px, nowStyle[i][1]+self.py])
x = newStyle[i][0]+self.px
y = newStyle[i][1]+self.py
DestList.append([x, y])
if x<0 or x>=HEIGHT or y<0 or y>=WIDTH : #or BL[x][y]==1 or LL[x][y].isactive==PASSIVE
Moveable=FALSE
if Moveable==TRUE:
for i in range(len(SourceList)):
self.Empty(SourceList[i][0],SourceList[i][1])
for i in range(len(DestList)):
self.Fill(DestList[i][0],DestList[i][1])
end :好了今日分享到这!陆续在分享干货!!
想领取更多完整源码可私信wo~~


